在ios8系统可以更新之后,好多人也第一时间更新了ios8,不幸的是,在我们的app内测的时候,用的企业证书ios8 好多用户无法接受APNS消息,这个怎么办?
debug才发现是没有获取到deviceToken,为什么呢?仔细看看代码:
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
在ios8 之后这个API 变了要这么写:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { UIUserNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:myTypes categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; }
这样写了感觉已经ok啦,没有想到再试了一次,还是不行,为什么呢?在仔细看了一次document,看来做事不要太急,一定要仔细,看清楚了在写。
这个都很重要
#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
加了上面的代码,这个完美啦,APNS 收到了。