registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later

前端 未结 15 1722
时光说笑
时光说笑 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:12

    I couldn't figure out what the "categories" NSSet variable should be set to, so if someone could fill me in I will gladly edit this post. The following does, however, bring up the push notification dialog.

    [[UIApplication sharedApplication] registerForRemoteNotifications];
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    

    Edit: I got a push notification to send to my phone with this code, so I'm not sure the categories parameter is necessary.

    0 讨论(0)
  • 2020-11-29 15:13

    Swift 2.0

    // Checking if app is running iOS 8
        if application.respondsToSelector("isRegisteredForRemoteNotifications") {
    
            print("registerApplicationForPushNotifications - iOS 8")
    
            application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil));
            application.registerForRemoteNotifications()
    
        } else {
            // Register for Push Notifications before iOS 8
            print("registerApplicationForPushNotifications - <iOS 8")
            application.registerForRemoteNotificationTypes([UIRemoteNotificationType.Alert, UIRemoteNotificationType.Badge, UIRemoteNotificationType.Sound])
    
        }
    
    0 讨论(0)
  • 2020-11-29 15:19

    After Xcode 6.1 Beta the code below works, slight edit on Tom S code that stopped working with the 6.1 beta (worked with previous beta):

       if UIApplication.sharedApplication().respondsToSelector("registerUserNotificationSettings:") {
            // It's iOS 8
            var types = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert
           var settings = UIUserNotificationSettings(forTypes: types, categories: nil)
           UIApplication.sharedApplication().registerUserNotificationSettings(settings)
        } else {
            // It's older
            var types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert
            UIApplication.sharedApplication().registerForRemoteNotificationTypes(types)
        }
    
    0 讨论(0)
  • 2020-11-29 15:19

    If all you need is the ios 8 code, this should do it.

     - (BOOL)application:(UIApplication *)application       didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
    {
           [application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound  | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)  categories:nil]];
    
           [application registerForRemoteNotifications];
    }
    
     return YES;
    }
    
    0 讨论(0)
  • 2020-11-29 15:23

    for iOS 8 and above

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
    [application registerUserNotificationSettings:settings];
    
    0 讨论(0)
  • 2020-11-29 15:27

    I think this is the better way to keep backwards compatibility if we go with this approach, it is working for my case and hope will work for you. Also pretty easy to understand.

    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)];
    }
    
    0 讨论(0)
提交回复
热议问题