有关ios10设备获取不到deviceToken的问题

2017-03-20  本文已影响1928人  新地球说着一口陌生腔调

看了好几个帖子在讨论相应的问题,我也是遇到了此问题查了很久没找出didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 不执行的原因。

其实很多文章没写清楚根本原因,除了或许代码原因、证书原因、还有其他原因的

首先我们来看看注册通知代码应该如何写

1、ios10应该这样写,首先导入框架 <UserNotifications/UserNotifications.h>

if([[[UIDevicecurrentDevice]systemVersion]floatValue]>=10.0) {

UNUserNotificationCenter*center = [UNUserNotificationCentercurrentNotificationCenter];

UNAuthorizationOptionsoptions =UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;

[centerrequestAuthorizationWithOptions:optionscompletionHandler:^(BOOLgranted,NSError*_Nullableerror) {

if(granted) {

[[UIApplicationsharedApplication]registerForRemoteNotifications];

NSLog(@"通知授权");

}

else{

}

}];

}

2、ios 8 到 ios 10之间这样写

if([application

respondsToSelector:@selector(registerUserNotificationSettings:)]) {

//注册推送,用于iOS8以及iOS8之后的系统

UIUserNotificationSettings*settings = [UIUserNotificationSettings

settingsForTypes:(UIUserNotificationTypeBadge|

UIUserNotificationTypeSound|

UIUserNotificationTypeAlert)

categories:nil];

[applicationregisterUserNotificationSettings:settings];

//[application registerForRemoteNotifications];

}

另外一个重要的步骤不要忘记了,在工程Target中的Push Notifications 的开关要开启!否则死活获取不到token。这点很多网友都没提及到。

希望对大家有帮助

上一篇下一篇

猜你喜欢

热点阅读