- device token的一些特性:
- 开发环境获取的
deviceToken和发布环境获取的deviceToken是不一样的 - 在一台设备中,
deviceToken是系统级别的,不同App获得的deviceToken是相同的 deviceToken会过期- 单个
App的更新deviceToken不会发生改变 - 当进行备份恢复、或恢复出厂设置之类的操作时,
deviceToken会发生改变,建议App在每次启动时都获取deviceToken - 用户抹除
iPhone的数据时,为了保护隐私,deviceToken会改变 - 升级系统
deviceToken有可能变化,猜测是升级大的系统版本后deviceToken会变化 - 在删除手机上的
App之后,再次下载安装,deviceToken在部分系统上会改变
注意: 推送相关证书只用在推送的后台即服务端使用,工程中只需打开推送相关开关即可,不需要推送证书
device token在iOS 13的变化
- 在
iOS 13之前的版本中,大部分这样处理1
2
3
4
5
6
7- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSString *dt = [deviceToken description];
dt = [dt stringByReplacingOccurrencesOfString: @"<" withString: @""];
dt = [dt stringByReplacingOccurrencesOfString: @">" withString: @""];
dt = [dt stringByReplacingOccurrencesOfString: @" " withString: @""];
NSLog(@"**发送给服务器的token字符串***:%@\n", dt);
} - 在
iOS 13之后的版本中,必须用以下方法处理(该方法在iOS 13之前也兼容)或者1
2
3
4
5
6
7
8
9- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSMutableString *deviceTokenString = [NSMutableString string];
const char *bytes = (const char *)deviceToken.bytes;
NSInteger count = deviceToken.length;
for (int i = 0; i < count; i++) {
[deviceTokenString appendFormat:@"%02x", bytes[i]&0x000000FF];
}
NSLog(@"**发送给服务器的token字符串***:%@\n", deviceTokenString);
}1
2
3
4
5
6
7
8
9- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
if (![deviceToken isKindOfClass:[NSData class]]) return;
const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
NSLog(@"**发送给服务器的token字符串***:%@\n",hexToken);
}