iOS 8 enabled device not receiving PUSH notifications after code update

后端 未结 8 2452
陌清茗
陌清茗 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 03:48
    The code below resolved:
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        UIUserNotificationType types;
        types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
        if (types & UIUserNotificationTypeAlert)
            pushEnabled=YES;
        else
            pushEnabled=NO;
    }
    else
    {
        UIRemoteNotificationType types;
        types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (types & UIRemoteNotificationTypeAlert)
           pushEnabled=YES;
        else
           pushEnabled=NO;
    
    }
    
    0 讨论(0)
  • 2020-11-27 03:53

    I tried to get the type information of the settings object and found out that the types property is basically a bitmask. Here is how you can extract the informations.

    My example is in Swift and >= iOS 8.0 though.

        let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
    
        if settings.types.rawValue & UIUserNotificationType.Alert.rawValue == UIUserNotificationType.Alert.rawValue
        {
           // can receive alert!
        }
        else
        {
           // if the user is not even able to receive alerts, show him a hint on how to reenable notifications in system settings
        }
    
        if settings.types.rawValue & UIUserNotificationType.Badge.rawValue == UIUserNotificationType.Badge.rawValue
        {
            // can receive badge!
        }
        if settings.types.rawValue & UIUserNotificationType.Sound.rawValue == UIUserNotificationType.Sound.rawValue
        {
            // can receive sound!
        }
    
    0 讨论(0)
  • 2020-11-27 03:54

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

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

    Its very simple:

    Add following line in didFinishLaunchingWithOptions of your project in xcode6

     [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
     [[UIApplication sharedApplication] registerForRemoteNotifications];
    
    0 讨论(0)
  • 2020-11-27 04:10

    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)];
        }
    }
    
    0 讨论(0)
  • 2020-11-27 04:10

    Try This if you have both iOS 8 and older versions:

    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)];
    }
    

    From Apple's Development site:

    UIUserNotificationSettings

    A UIUserNotificationSettings object encapsulates the types of notifications that can be displayed to the user by your app. Apps that use visible or audible alerts in conjunction with a local or push notification must register the types of alerts they employ. UIKit correlates the information you provide with the user’s preferences to determine what types of alerts your app is allowed to employ.

    Use this class to encapsulate your initial registration request and to view the request results. After creating an instance of this class and specifying your preferred settings, call the registerUserNotificationSettings: method of the UIApplication class to register those settings. After checking your request against the user preferences, the app delivers the results to the application:didRegisterUserNotificationSettings: method of its app delegate. The object passed to that method specifies the types of notifications that your app is allowed to use.

    In addition to registering your app’s alert types, you can also use this class to register groups of custom actions to display in conjunction with local or push notifications. Custom actions represent immediate tasks your app can perform in response to the notification. You define groups of actions and associate the entire group with a given notification. When the corresponding alert is displayed, the system adds buttons for each action you specified. When the user taps the button for one of the actions, the system wakes your app and calls the application:handleActionWithIdentifier:forRemoteNotification:completionHandler: or application:handleActionWithIdentifier:forLocalNotification:completionHandler: method of its app delegate. Use those methods to perform the requested action.

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