Get Device Token in iOS 8

前端 未结 8 2272
借酒劲吻你
借酒劲吻你 2020-12-05 05:12

I need device token to implement push notification in my app.
How can I get device token since didRegisterForRemoteNotificationsWithDeviceToken method is no

相关标签:
8条回答
  • 2020-12-05 06:09

    Here is the solution.

    in applicationDidFinishLaunchingWithOptions:

    {
    
     UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    }
    
    
    - (void)application:(UIApplication*)application didRegisterUserNotificationSettings:(nonnull UIUserNotificationSettings *)notificationSettings
    {
        [application registerForRemoteNotifications];
    }
    
    
    - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(nonnull NSError *)error
    {
        NSLog(@" Error while registering for remote notifications: %@", error);
    }
    
    - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken
    {
      NSLog(@"Device Token is : %@", deviceToken);
    }
    
    0 讨论(0)
  • 2020-12-05 06:11

    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

    #ifdef __IPHONE_8_0
      //Right, that is the point
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
    |UIRemoteNotificationTypeSound
    |UIRemoteNotificationTypeAlert) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    #else
        //register to receive notifications
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
    #endif
    

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