IOS 8 Remote Notification

How can I get the device token for remote notification in iOS 8? I used the didRegisterForRemoteNotificationsWithDeviceToken method in AppDelegate on iOS <8 and it returned the device token. But in iOS 8, this is not the case.

+76
objective-c ios8 apple-push-notifications
Jun 14 '14 at 4:40
source share
7 answers

Read the code in UIApplication.h.

You will know how to do it.

Firstly:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

add code like this

 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { #ifdef __IPHONE_8_0 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]; [application registerUserNotificationSettings:settings]; #endif } else { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [application registerForRemoteNotificationTypes:myTypes]; } 

if you are not using both Xcode 5 and Xcode 6 , try this code

 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert) categories:nil]; [application registerUserNotificationSettings:settings]; } else { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [application registerForRemoteNotificationTypes:myTypes]; } 

(Thanks for @zeiteisen @dmur)




Secondly:

Add this function

 #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 

And you can get deviceToken in

 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 

if it still does not work, use this function and NSLog error

 -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
+180
Jun 30 '14 at 10:57
source share

Registration method for iOS 8 and support for older versions

 UIApplication *application = [UIApplication sharedApplication]; if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound |UIUserNotificationTypeAlert) categories:nil]; [application registerUserNotificationSettings:settings]; } else { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [application registerForRemoteNotificationTypes:myTypes]; } 

and in the application delegate add

 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { [application registerForRemoteNotifications]; } 

iOS8 can receive silent notifications without asking permission. The call - (void)registerForRemoteNotifications . After this application:didRegisterForRemoteNotificationsWithDeviceToken: will be called

Note: a token callback is only called if the application has successfully registered to notify users using the function below or if the background update function is enabled.

Check the settings for your application if the notification type is enabled. If not, you will not receive the device token.

You can now receive silent notifications using

 aps { content-available: 1 } 

in notification payload

But the notifications that appear still need permission. Call

 UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert; UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [application registerUserNotificationSettings:notificationSettings]; 

This code must request permission.

You should now be prepared to receive push notifications.

+75
Jun 20 '14 at 14:16
source share

In my case, I made the necessary updates to request push notifications for iOS 7 and iOS 8, however I did not implement a new callback when an iOS 8 user provides access. I needed to add this method to my application delegate.

 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { [application registerForRemoteNotifications]; } 
+15
Sep 27 '14 at 23:02
source share

If you use Xamarin.iOS to create your mobile application, you can use this code snippet to request registration of push notifications

 if (UIDevice.CurrentDevice.CheckSystemVersion(8,0)) { UIUserNotificationType userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound; UIUserNotificationSettings settings = UIUserNotificationSettings.GetSettingsForTypes(userNotificationTypes, null); UIApplication.SharedApplication.RegisterUserNotificationSettings(settings); } else { UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound; UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes); } 

You also need to override the DidRegisterUserNotificationSettings method to get the device token returned from the APNS server:

 public override void DidRegisterUserNotificationSettings(UIApplication application, UIUserNotificationSettings notificationSettings) { application.RegisterForRemoteNotifications(); } 
+1
Sep 30 '14 at 22:02
source share

Madao's answer ( https://stackoverflow.com/a/167189/ ) is right, but ....

 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge 

should be more "correct"

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

These flags have the same bitmask values, and therefore both will work, but UIUserNotificationSettings requires UIUserNotificationType not UIRemoteNotificationType .

In addition, I would call

 [application registerUserNotificationSettings:settings]; 

In the AppDelegate method (depending on the granted rights)

 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 
+1
Nov 12 '14 at 10:27
source share

I believe that the best way to maintain backward compatibility is possible with this approach, it works for my business, I hope 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
Jan 04 '15 at 7:48
source share
 UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; [application registerForRemoteNotifications]; 
0
May 11 '15 at 12:25
source share



All Articles