0
votes

I'm trying to add push notifications to my iOS app using Parse.com push notification service, but I'm running into problems with some of my devices not receiving notifications.

Current code

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    Parse.setApplicationId("*****", clientKey: "*****")

    // Register for Push Notitications
    if application.applicationState != UIApplicationState.Background {
        // Track an app open here if we launch with a push, unless
        // "content_available" was used to trigger a background push (introduced in iOS 7).
        // In that case, we skip tracking here to avoid double counting the app-open.

        let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
        let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
        var pushPayload = false
        if let options = launchOptions {
            pushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
        }
        if (preBackgroundPush || oldPushHandlerOnly || pushPayload) {
            PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
        }
    }
    if application.respondsToSelector("registerUserNotificationSettings:") {
        let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    }else {
        let types : UIRemoteNotificationType = [.Badge, .Alert, .Sound]
        application.registerForRemoteNotificationTypes(types)
    }
    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)
}

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 seems to work on some devices (tested it on coworker's iPhone 5 - worked, tested on my boss' iPhone 6 - didn't work)

The code I currently use also gives me 2 warnings, which is (what I suspect to be) the cause of push notifications not working on every iDevice.

Warning 1:

/Users/ds/code/rp-iOS/rp/AppDelegate.swift:43:25: 'UIRemoteNotificationType' was deprecated in iOS 8.0: Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.

Line 43:

let types : UIRemoteNotificationType = [.Badge, .Alert, .Sound]

Warning 2:

/Users/ds/code/rp-iOS/rp/AppDelegate.swift:44:25: 'registerForRemoteNotificationTypes' was deprecated in iOS 8.0: Please use registerForRemoteNotifications and registerUserNotificationSettings: instead

Line 44:

application.registerForRemoteNotificationTypes(types)

Now I'm confused about these warnings because I copied the code from the Parse.com documentation - so what am I doing wrong? It seems like this else clause provides backwards compatibility for devices that run older versions of iOS.

In my Parse.com Push console, I also get some errors:

Parse.com push console

Any help would be greatly appreciated.

1
You need to save your PFInstallation after you set its device token - Paulw11
Can't believe I missed that from the documentation. Thank you. - nbokmans

1 Answers

1
votes

In didRegisterForRemoteNotificationsWithDeviceToken try to add this line of code.

installation.saveInBackground()

To get rid of the warnings I suggest using this code (I've put that in static function for convenience and ability to reuse)

static func askForPushNotifications(){
  let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
  UIApplication.sharedApplication().registerUserNotificationSettings(settings)
  UIApplication.sharedApplication().registerForRemoteNotifications()
}