通过真机调试以下代码:开发环境获取的deviceToken和发布环境获取的deviceToken当然是不一样的!
而且在开发环境和发布环境中如果开发证书不一样的话获取的deviceToken也不一样,亲测!!!
文章末尾讨论发布环境的deviceToken 。。。
1
2
3
4
5
6
7
8
9
10
11- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] init]; self.window.backgroundColor = [UIColor whiteColor]; self.window.frame = [[UIScreen mainScreen] bounds]; [self registerNotification:application]; [self.window makeKeyAndVisible]; return YES; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16- (void)registerNotification:(UIApplication *)application { if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8) { if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [application registerUserNotificationSettings:settings]; [application registerForRemoteNotifications]; } }else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; NSLog(@"not isIOS8"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35///Token值成功获取的时候走的是这个方法(Token值不能带空格) -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"deviceToken----------->%@",deviceToken); NSString *pushToken = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""] ; NSLog(@"pushToken----------->%@",pushToken); } ///Token值获取失败的时候走的是这个方法 -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"Token值获取失败,error--->%@",error); } ///应用程序处在打开状态,且服务器有推送消息过来时,以及通过推送打开应用程序,走的是这个方法 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { for (id key in userInfo) { NSLog(@"%@:%@",key, [userInfo objectForKey:key]); } ///Icon推送数量设为0 application.applicationIconBadgeNumber=0; }
现在来讨论发布环境的deviceToken,之前我认为是不会改变的,但后来发现貌似会变。当然这个调试就需要去回复iPhone手机了,如果你愿意可以去试试,弄个不用的iPhone4,iPhone4s之类的。
if a device is wiped, it will get a new device token. (如果一个设备被清除,它将获得一个新的设备令牌。)
官方网站是这样写的: If the user restores backup data to a new device or computer, or reinstalls the operating system, the device token changes
正是因为device有可能改变,所以官方描述:An application should register every time it launches and give its provider the current token
所以...不要判断APP里是否已经保存的deviceToken 就不去register了, should every time !!
最后
以上就是舒适外套最近收集整理的关于iOS APNS device Token 是否会改变?的全部内容,更多相关iOS内容请搜索靠谱客的其他文章。
发表评论 取消回复