registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later
For iOS<10
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{ //-- Set Notification if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) { // iOS 8 Notifications [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [application registerForRemoteNotifications]; } else { // iOS < 8 Notifications [application registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)]; } //--- your custom code return YES;}
For iOS10
As you described, you will need to use a different method based on different versions of iOS. If your team is using both Xcode 5 (which doesn't know about any iOS 8 selectors) and Xcode 6, then you will need to use conditional compiling as follows:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { // use registerUserNotificationSettings} else { // use registerForRemoteNotificationTypes:}#else// use registerForRemoteNotificationTypes:#endif
If you are only using Xcode 6, you can stick with just this:
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { // use registerUserNotificationSettings} else { // use registerForRemoteNotificationTypes:}
The reason is here is that the way you get notification permissions has changed in iOS 8. A UserNotification
is a message shown to the user, whether from remote or from local. You need to get permission to show one. This is described in the WWDC 2014 video "What's New in iOS Notifications"
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)}