今天最近做了个远程推送功能,未使用第三方开发。发现一个问题:在保证证书正确、网络连接正常、首次进入允许通知的情况下,获取不到APNs的deviceTonken回调。
既不调用application:didRegisterForRemoteNotificationsWithDeviceToken;方法
也不调用application:didFailToRegisterForRemoteNotificationsWithError:方法。
网上搜了不少资料,结果大家给的解决方案几乎都一致,把原因引向了iOS8.0+和iOS7系统问题,并给出根据不同系统版本对remotNotifi进行注册的方法:
iOS8.0+ registerUserNotificationSettings:
iOS8.0- registerForRemoteNotificationTypes:
(还有人说自己也不知道怎么了,后来就突然好了!!!!)
找了很久很久很久...,都特么都是这个方法。
我想说:这特么谁不知道。
被逼无奈,我只能一步一步的从申请证书到代码编写一步一步的检查,一步一步的看...还是没找到原因。我累个艹了。
没办法了,只能从appdelegate代理方法入手,找原因和解决方法。
原因是没找到,但是解决方法却找到了,直接贴代码:
// This callback will be made upon calling -[UIApplication registerUserNotificationSettings:]. The settings the user has granted to the application will be passed in as the second argument.
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
iOS8.0以后可以用这个作为second argument。然后:
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
[application registerForRemoteNotifications];
}
果断解决了,什么deviceToken问题都没有了。
一个推送做的我心累,虽然我不知道什么原因导致的获取不到deviceToken,但是问题总算解决了。
代码:
//remote 授权
- (void)registRemoteNotification{
#ifdef __IPHONE_8_0
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
#else
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif
}
#pragma mark - remote Notification
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken{
NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"device token is %@",token);
[[NSUserDefaults standardUserDefaults] setValue:token forKey:@"video_deviceToken"];
}
- (void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"%@",error);
}
//ios 7.0
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"did recevive remote noti userInfo %@ for ios 7 ",userInfo);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
/**
* 系统会估量App消耗的电量,并根据传递的UIBackgroundFetchResult 参数记录新数据是否可用
* 调用完成的处理代码时,应用的界面缩略图会自动更新
*/
NSLog(@"did Receive Remote Notification userInfo %@",userInfo);
switch (application.applicationState) {
case UIApplicationStateActive:
completionHandler(UIBackgroundFetchResultNewData);
break;
case UIApplicationStateInactive:
completionHandler(UIBackgroundFetchResultNewData);
break;
case UIApplicationStateBackground:
completionHandler(UIBackgroundFetchResultNewData);
break;
default:
break;
}
}