registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later, so?

Some body must have faced the problem like me that theĀ registerForRemoteNotificationTypes: doesn’t works on iOS 8 or later device. I found the solution and I think this is the better way to keep backwards compatibility and make workable the remote notification for iOS 8.0 and later versions if we go with this approach, it is working for my case and will hope work for you. Also pretty easy to understand.

So to register write this code in AppDelegate’s - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { method:


    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
             (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }

Now you will get a confirmation dialog at the starting time. if you all this app for push notifications, then you will get the device token for notification service from following delegate:(if supports all the versions we have)

#pragma mark - Notification delegates
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"content---%@", token);
    
}

If it fails for any reason like certification error or any other reason, following delegate will be called.

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    
    NSLog(@"Error %@", err);
    
}

So, now you have device token and you can do anything you like..

Advertisements