iOS 8 enabled device not receiving PUSH notifications after code update iOS 8 enabled device not receiving PUSH notifications after code update ios ios

iOS 8 enabled device not receiving PUSH notifications after code update


The way to register for push notifications has been changed in iOS 8:Below is the code for all versions till iOS 9:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]){    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];}else{    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];}

In case you want to check whether push notifications are enabled or not use below code:

- (BOOL) pushNotificationOnOrOff{    if ([UIApplication instancesRespondToSelector:@selector(isRegisteredForRemoteNotifications)]) {        return ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]);    } else {        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];        return (types & UIRemoteNotificationTypeAlert);    }}#ifdef __IPHONE_8_0- (void)application:(UIApplication *)application   didRegisterUserNotificationSettings:   (UIUserNotificationSettings *)notificationSettings{    //register to receive notifications    [application registerForRemoteNotifications];}- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString   *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler{    //handle the actions    if ([identifier isEqualToString:@"declineAction"]){    }    else if ([identifier isEqualToString:@"answerAction"]){    }}#endif

Above code will run on Xcode 6+ only...


Add this line at the top of your .m file

 #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

1) You have to put condition when you register for push notification in IOS8. add this code in application did finish launch.

if(IS_IOS_8_OR_LATER) {    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: (UIRemoteNotificationTypeBadge                                                                                         |UIRemoteNotificationTypeSound                                                                                       |UIRemoteNotificationTypeAlert) categories:nil];    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; } else {    //register to receive notifications    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; }

2) Then add this methods for IOS8

#ifdef __IPHONE_8_0- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:   (UIUserNotificationSettings *)notificationSettings{    //register to receive notifications    [application registerForRemoteNotifications];}- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString   *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler{    //handle the actions    if ([identifier isEqualToString:@"declineAction"]){    }    else if ([identifier isEqualToString:@"answerAction"]){    }}#endif

Then you notification delegate method will be called. hope this will help you !!!


Runtime and old compiler safe options ... if you run in old xcode (5.0 or earlier)

    // changes of API in iOS 8.0- (void) registerForPushNotification{    NSLog(@"registerForPushNotification");    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)    {#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 //__IPHONE_8_0 is not defined in old xcode (==0). Then use 80000        NSLog(@"registerForPushNotification: For iOS >= 8.0");        [[UIApplication sharedApplication] registerUserNotificationSettings:            [UIUserNotificationSettings settingsForTypes:                (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)                                              categories:nil]];        [[UIApplication sharedApplication] registerForRemoteNotifications];#endif    } else {        NSLog(@"registerForPushNotification: For iOS < 8.0");        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:         (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];    }}