I'm trying to add parse to my appDelegate with Swift. I get an error saying
Cannot invoke 'registerForRemoteNotifications' with an argument list of type '(UIUserNotificationType)'
Here's my code. What's wrong?
if application.respondsToSelector("registerUserNotificationSettings:") {
let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
let types = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound
application.registerForRemoteNotifications(types)
}
return true
After you've followed the Parse tutorial for setting up Push Notifications and Certificates with the Apple Developer Console, make sure your AppDelegate.swift
looks like this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
Parse.setApplicationId("ID", clientKey:"KEY")
let userNotificationTypes = (UIUserNotificationType.Alert |
UIUserNotificationType.Badge |
UIUserNotificationType.Sound);
let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
// Store the deviceToken in the current Installation and save it to Parse
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.addUniqueObject("Chat", forKey: "channels")
installation.saveInBackground()
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
if error.code == 3010 {
print("Push notifications are not supported in the iOS Simulator.")
} else {
print("application:didFailToRegisterForRemoteNotificationsWithError: %@", error)
}
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
}
This last function resets the badge counter when the user opens the app:
func applicationDidBecomeActive(application: UIApplication) {
//Reset badge counter to zero
var currentInstallation = PFInstallation.currentInstallation()
if(currentInstallation.badge != 0){
currentInstallation.badge = 0
}
}
let settings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
This could be useful if you're using Swift 2.0, due to some error saying
binary operator cannot apply to two UIUserNotificationType
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.