Changes in iOS 8 Push Notifications

iOS 8 brings some new features and changes, and among interesting changes are the new possibilities of app push notifications. In iOS 8, notifications will be a lot more interactive and will allow the user to engage with them to do certain actions without leaving the app which he or she is currently using, this gives us developers interesting range of possibilities to interact with our users through push or local notifications, for example to allow the user to post on Facebook or Twitter through the notification center, or to write back to a friend through a messaging app without even opening it. Of course with the new possibilities come the API changes, and one of them is the registering process to the APNS (Apple Push Notification Service). Although the changes are not big, they may leave a place to wonder what exactly needs to be changed in the code, in order to make it work with the API changes.

Before iOS 8, the app registration to the APNS and to push notifications was done through the method registerForRemoteNotificationTypes:(UIRemoteNotificationType), with UIRemoteNotificationTypeBadge, UIRemoteNotificationTypeAlert and UIRemoteNotificationTypeSound usually as parameters, this would tell our app that we would like to register to the APNS and to enable push notifications. The app would then ask the user if he or she would like the app to send them push notifications, on approval the method didRegisterForRemoteNotificationsWithDeviceToken would be called and from there we continued on to registering the device to our server through the device token.

In iOS 8, the method
registerForRemoteNotificationTypes:(UIRemoteNotificationType)
is no longer supported, for those of you fellow developers who already tried, I’m sure you noticed the log message you get once you use the method to register the app to push notifications. The second part of the registration to the APNS in iOS 8 stays the same, once the user approves the usage of push notifications in the App, the method didRegisterForRemoteNotificationsWithDeviceToken will be called and from there you continue as usual.

The first part of the registration to the APNS in iOS 8 is done through the new class UIUserNotificationSettings, an Object that one init method:
+ settingsForTypes:categories:
the first parameter indicates the normal types which we already know, UIRemoteNotificationTypeBadge, UIRemoteNotificationTypeAlert etc,
the second parameter is a NSSet object of the UIUserNotificationCategory object that you can specify when creating the settings object. Each object can corresponds to a group of actions that you may choose to display in the conjunction of the push notification and will have you initial request after the registration.

If you are intending to create a richer, more interactive push notification you need to specify the actions through the Categories parameter, if what you are looking for is the normal ordinary push notification, the Categories parameter can be set to nil.
After creating the UIUserNotificationSettings object with your specifications the method – registerUserNotificationSettings: needs to be called with the
UIUserNotificationSettings object that you already created as the parameter.
Once done, the only thing left for you to do is to call the method registerForRemoteNotifications (notice that it is called registerForRemoteNotifications and not registerForRemoteNotificationsTypes like before), and from that point you can continue on normally as usual.

In your code, you will have to check the iOS version in order to determine which methods to use, if you and/or your team are using only Xcode 6, this piece of code will be enough:

 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
       UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound
                                                                                categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         UIRemoteNotificationTypeBadge |
         UIRemoteNotificationTypeAlert |
         UIRemoteNotificationTypeSound];

    }

If you and/or your team are using Xcode 6 and 5, the code above will not suffice because Xcode 5 will not recognise the new selectors from the new API is not recognised. So in that case you should use the following code :

 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
       UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound
                                                                                categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         UIRemoteNotificationTypeBadge |
         UIRemoteNotificationTypeAlert |
         UIRemoteNotificationTypeSound];

    }
    #else
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeBadge |
     UIRemoteNotificationTypeAlert |
     UIRemoteNotificationTypeSound];

    #endif

The preproccesor command #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 tells Xcode to check what is the max iOS version that is supported, and to compile the code just in case where the version is equal or higher to 8. In Xcode 5, the last allowed version is 7, so once the compiler sees that the version 8 is not supported it will jump directly to the #else. In Xcode 6, the exact opposite.

And that’s it! As I mentioned before, that specific change concerning iOS 8 Push Notifications is not that big of a deal, but we all need guidance at some point or another. So where to now? For more information here is a link to apple’s documentation and to WWDC What’s New In iOS Notifications. For those of you who are interested, here is a really good tutorial on how to use the categories and action to create richer and more complex notifications. Don’t hesitate to contact us regarding any question or opinions on our contact page.

Happy coding 🙂

Schreibe einen Kommentar

Time limit is exhausted. Please reload CAPTCHA.