0

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
3
  • You're wrote two times ` application.registerForRemoteNotifications(OoO)` but each times differently. One with nothing as OoO, the other one with a flags of UIUserNotificationSettings. The last one seems wrong according to your message. Seems that you wanted :registerForRemoteNotificationTypes(types) (in the else test) Commented Jul 27, 2015 at 14:04
  • The latter - so Xcode says - is not working... Commented Jul 27, 2015 at 14:06
  • 1
    Because types changed too in iOS8. Before, it was UIRemoteNotificationType and not UIUserNotificationType => let types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Sound application.registerForRemoteNotificationTypes(types)} Commented Jul 27, 2015 at 14:08

2 Answers 2

1

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.