Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote Notification iOS 8

How can I get the Device Token for remote notification in iOS 8? I used the method didRegisterForRemoteNotificationsWithDeviceToken in AppDelegate in iOS < 8, and it returned the device token. But in iOS 8, it does not.

like image 434
quang thang Avatar asked Jun 14 '14 04:06

quang thang


People also ask

What is remote notification in iOS?

Overview. Use remote notifications (also known as push notifications) to push small amounts of data to devices that use your app, even when your app isn't running. Apps use notifications to provide important information to users. For example, a messaging service sends remote notifications when new messages arrive.

How do I turn on remote notifications on my iPhone?

Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered — immediately or in the scheduled notification summary.


1 Answers

Read the code in UIApplication.h.

You will know how to do that.

First:

- (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 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 's remind)


Second:

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 your can get the deviceToken in

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

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

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
like image 84
Madao Avatar answered Sep 21 '22 12:09

Madao