您的位置:首页 > 移动开发 > IOS开发

iOS APNS device Token 是否会改变?

2015-09-21 10:20 519 查看
通过真机调试以下代码:[b]开发环境获取的deviceToken和发布环境获取的deviceToken当然是不一样的![/b]

而且在开发环境和发布环境中如果开发证书不一样的话获取的deviceToken也不一样,亲测!!!

[b]文章末尾讨论发布环境的deviceToken 。。。 [/b]

- (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
Push Notification注意事项
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: