概述
通过真机调试以下代码:开发环境获取的deviceToken和发布环境获取的deviceToken当然是不一样的!
而且在开发环境和发布环境中如果开发证书不一样的话获取的deviceToken也不一样,亲测!!!
文章末尾讨论发布环境的deviceToken 。。。
- (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;
}
- (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");
}
}
///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 APNS device Token 是否会改变?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复