获取iOS 8中的设备令牌

我需要设备令牌在我的应用程序中实现推送通知。
我怎样才能得到设备令牌,因为didRegisterForRemoteNotificationsWithDeviceToken方法不在iOS 8上工作。我在应用程序委托中尝试此代码,但它不给我设备令牌。

  [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; 

阅读UIApplication.h中的代码。

你会知道如何做到这一点。

第一:

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

像这样添加代码

 #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 

如果您不使用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]; } 

(感谢@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 

你可以得到deviceToken

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

如果它仍然不起作用,使用这个函数和NSLog的错误

 -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 

如果您在旧iOS版本中发生崩溃,请在@ Madao的响应中附加一个小validation:

 #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]; 

macros__IPHONE_8_0只是允许你在__IPHONE_8_0 / iOS的旧版本中进行编译,不会得到编译错误或警告,但是在iOS 7或更低版​​本的设备上运行代码会导致崩溃。

在iOS8 +中获取设备令牌

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //This code will work in iOS 8.0 xcode 6.0 or later if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeNewsstandContentAvailability| UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; } return YES; } - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { NSString* deviceToken = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""] stringByReplacingOccurrencesOfString: @" " withString: @""] ; NSLog(@"Device_Token -----> %@\n",deviceToken); } 

除了这个问题居高临下的答案外,对于已经实现了所有必需的委托方法的知情开发人员来说,这个问题最可能的原因是他们正在使用通配符configuration文件 (为什么不呢?它使创build和testing开发应用程序变得如此简单!)

在这种情况下,您可能会看到错误Error Domain=NSCocoaErrorDomain Code=3000 "no valid 'aps-environment' entitlement string found for application"

为了testing通知,您实际上必须回到2000年以后,login到developer.apple.com,并设置您的应用程序特定的供应configuration文件,并启用推送通知。

  1. 创build与您的应用程序包标识符相对应的应用程序ID。 一定要检查“推送通知”(目前从底部第二个选项)。
  2. 为该App ID创build一个供应configuration文件。
  3. 通过其余的可怕的供应步骤,我们都爱忘记。
  4. 利润!

在您的开发者帐户上,确保您的应用程序ID已正确设置了推送通知,然后您需要重新生成并下载您的供应configuration文件。 我的问题是,我已经下载了configuration文件,但xcode运行不正确的。 要解决这个问题,请转到目标构build设置,向下滚动到代码签名,在configurationconfiguration文件部分下,确保您使用的configurationconfiguration文件与您生成的configuration文件的名称相匹配(如果有的话,应该有一个带有选项的下拉菜单已经安装了多个)。

这是解决scheme。

在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); } 

@madoa答案是绝对正确的。 只要注意, 它不在模拟器中工作

在这种情况下

 -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 

被调用时出现错误REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR