iOS 8 enabled device not receiving PUSH notifications after code update

后端 未结 8 2453
陌清茗
陌清茗 2020-11-27 03:45

I recently upgraded one of my test iphones to iOS 8 and then upgraded the PUSH registration code as below (using xCode 6)

-(BOOL)hasNotificationsEnabled {

          


        
相关标签:
8条回答
  • 2020-11-27 04:11
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        // For iOS 8
    
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    
     [[UIApplication sharedApplication] registerForRemoteNotifications];
    } 
    
    else 
    {
        // For iOS < 8
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }
    

    After this:

    1. Delete the apps who don't have Push notifications any more form the device.
    2. Turn the device off completely and turn it back on.
    3. Go to settings and set date and time ahead a day
    4. Turn the device off again and turn it back
    5. Install the apps again and It'll ask for notification like the first time you installed It.
    6. Reset your date / time again if not automatically set.

    This will reset the notification settings. Re install the app and all will work fine :)

    0 讨论(0)
  • 2020-11-27 04:15

    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...

    0 讨论(0)
提交回复
热议问题