在iOS 8中获取设备令牌

Pan*_*kaj 18 ios ios8

我需要设备令牌才能在我的应用中实现推送通知.
我怎么能获得设备令牌,因为didRegisterForRemoteNotificationsWithDeviceToken方法在iOS 8上不起作用.我在app delegate中尝试了这个代码,但它没有给我设备令牌.

    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
Run Code Online (Sandbox Code Playgroud)

Mad*_*dao 56

阅读UIApplication.h中的代码.

你会知道怎么做.

第一:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Run Code Online (Sandbox Code Playgroud)

像这样添加代码

#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
Run Code Online (Sandbox Code Playgroud)

如果您不同时使用Xcode 5和Xcode 6,请尝试此代码

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];
}
Run Code Online (Sandbox Code Playgroud)

(感谢@zeiteisen @dmur的提醒)


第二:

添加此功能

#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
Run Code Online (Sandbox Code Playgroud)

你可以获得deviceToken

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
Run Code Online (Sandbox Code Playgroud)

如果它仍然不起作用,使用此功能和NSLog 错误

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
Run Code Online (Sandbox Code Playgroud)

  • 这将在iOS 8以下的iOS版本上崩溃.#ifdef是一个COMPILER函数,在编译时进行评估.你还需要运行时检查! (10认同)
  • 您可以在方法内部使用`if(([[[[[[[[[[[[[[[UIDevice currentDevice] systemVersion] floatValue]> = 8.0 && [[[[UIDevice currentDevice] systemVersion] floatValue] <9.0))` (2认同)
  • 我试过了,但是 (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 由于某种原因在 iOS 8 上没有被调用。(至少我的命令,包括 NSLog 都没有通过。)任何可能的解决方案? (2认同)

Jua*_*rre 11

如果您在较旧的iOS版本上崩溃,请对@ Madao的响应附加一个小验证:

#ifdef __IPHONE_8_0
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
#endif

UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound |     UIRemoteNotificationTypeNewsstandContentAvailability;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
Run Code Online (Sandbox Code Playgroud)

什么是__IPHONE_8_0宏观不只是让您在旧版本的Xcode/iOS版的,你没有得到的编译错误或警告编译,但与iOS 7或更低的设备上运行的代码会导致崩溃.

  • 我做了这个,它工作,如果([application respondsToSelector:@selector(registerUserNotificationSettings :)]){UIUserNotificationSettings*settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert类别:nil]; [application registerUserNotificationSettings:settings]; } else {[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; } (2认同)