registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later

前端 未结 15 1724
时光说笑
时光说笑 2020-11-29 15:09

When trying to register for push notifications under iOS 8.x:

application.registerForRemoteNotificationTypes(UIRemoteNotificationType.Alert | UIRemoteNotific         


        
相关标签:
15条回答
  • 2020-11-29 15:34

    Building on @Prasath's answer. This is how you do it in Swift:

    if application.respondsToSelector("isRegisteredForRemoteNotifications")
    {
        // iOS 8 Notifications
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: (.Badge | .Sound | .Alert), categories: nil));
        application.registerForRemoteNotifications()
    }
    else
    {
        // iOS < 8 Notifications
        application.registerForRemoteNotificationTypes(.Badge | .Sound | .Alert)
    }
    
    0 讨论(0)
  • 2020-11-29 15:34

    For the Swift-inclined:

    if let registration: AnyObject = NSClassFromString("UIUserNotificationSettings") { // iOS 8+
        let notificationTypes: UIUserNotificationType = (.Alert | .Badge | .Sound)
        let notificationSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
    
        application.registerUserNotificationSettings(notificationSettings)
    } else { // iOS 7
        application.registerForRemoteNotificationTypes(.Alert | .Badge | .Sound)
    }
    
    0 讨论(0)
  • 2020-11-29 15:35

    You can use this

    if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
        {
            // for iOS 8
            [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    
            [application registerForRemoteNotifications];
        }
        else
        {
            // for iOS < 8
            [application registerForRemoteNotificationTypes:
             (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
        }
    
        // RESET THE BADGE COUNT 
        application.applicationIconBadgeNumber = 0;
    
    0 讨论(0)
提交回复
热议问题