Push management on iOS8

To migrate an app that uses push notifications from iOS7 to iOS8, you must change some methods.

Register the app to receive push

The method registerForRemoteNotificationTypes has been deprecated, and now you must use registerUserNotificationSettings. To have compatibility with both versions you can use this code:

- (void) registerDeviceForNotifications
{
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
        // iOS 8 Notifications
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        // iOS < 8 Notifications
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }
    
    
}

Check if the user has the push enabled

To check if the user has accepted to receive push, and has not disable it at the device settings, on iOS7 you could use the method enabledRemoteNotificationTypes. But with iOS8, it has been also deprecated. At iOS8 there are two methods to check that.

isRegisteredForRemoteNotifications with this method you can know if the app has prompted the dialog “Do you want to receive push notifications?”, but you can’t know if the user has disabled them.

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

So there is another method currentUserNotificationSettings that seems similar to the old method enabledRemoteNotificationTypes, but works a little bit different.

UIUserNotificationSettings *noticationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
 
if (!noticationSettings || (noticationSettings.types == UIUserNotificationTypeNone)) {
    return NO;
}
return YES;

If you want to keep compatibility with iOS8 and previous versions you can use that method to check if the user has the push enabled:

- (BOOL) areNotificationsEnabled
{
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){
        UIUserNotificationSettings *noticationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if (!noticationSettings || (noticationSettings.types == UIUserNotificationTypeNone)) {
            return NO;
        }
        return YES;
    }
 
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types & UIRemoteNotificationTypeAlert){
        return YES;
    } else{
        return NO;
    }
}

Library

I haven’t used it, but there is a library that seems to make this management easier ISHPermissionKit. It works with CocoaPods so it can be added easily to your project.

I’ve used the code of this library to get the iOS8 part of the code to check if the user have the push enabled.

About Néstor Malet
I’m a web and iOS developer. I like to start projects, personal ones, with friends or at work.

One thought on “Push notifications iOS8

  1. You just saved my life. Thank you very very very much.

    I had to publish an app today (it had to be today!) and we didn’t have our iOS developer to help with this.

    Great explanation :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>