首页 > 其他分享 >适配 iOS 13 设置 deviceToken

适配 iOS 13 设置 deviceToken

时间:2023-02-25 14:03:28浏览次数:50  
标签:13 tokenBytes ntohl 适配 iOS NSString application token deviceToken


在 iOS 13 之前的版本使用下面代码可以将获取到的 deviceToken,转为 NSString 类型,并去掉其中的空格和尖括号,作为参数传入 setDeviceToken: 方法中。

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *token = [deviceToken description];
token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
[[RCIMClient sharedRCIMClient] setDeviceToken:token];
}

但是上面这段代码在 iOS 13 上已经无法获取准确的 deviceToken 了,iOS 13 的设备通过 [deviceToken description] 获取的内容如下:

{length=32,bytes=0xfe270338e80825d6c5d1754096f916b8...0d19a5d5834cff75}

解决方法:

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
if (@available(iOS 13.0, *)) {
NSString *token = [self getHexStringForData:deviceToken];
NSLog(@"iOS 13之后的deviceToken的获取方式");
} else {
NSString *deviceTokenString = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"iOS 13之前的deviceToken的获取方式");
}
}

- (NSString *)getHexStringForData:(NSData *)data {
NSUInteger len = [data length];
char *chars = (char *)[data bytes];
NSMutableString *hexString = [[NSMutableString alloc] init];
for (NSUInteger i = 0; i < len; i ++) {
[hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]];
}
return hexString;
}

或者

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
const unsigned *tokenBytes = [deviceToken bytes];
if (tokenBytes == NULL) {
return;
}
NSString *token = [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])];

}

Swift 处理方法:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
var deviceTokenString = String()
let bytes = [UInt8](deviceToken)
for item in bytes {
deviceTokenString += String(format: "%02x", item&0x000000FF)
}
// 设置deviceTokenString 并发送到服务器
}


标签:13,tokenBytes,ntohl,适配,iOS,NSString,application,token,deviceToken
From: https://blog.51cto.com/u_14062833/6085432

相关文章

  • iOS日志记录和异常捕获
    日志记录iOS日志记录当前文件的堆栈、类名、函数名、行号及文件路径等信息NSArray*array=[NSThreadcallStackSymbols];NSLog(@"堆栈信息:%@",array);NSLog(@"当......
  • 设计模式之适配器模式
    简介当我们需要使用某个对象的功能,但是我们没有这个对象时,我们可以用适配器和替换对象来实现这个功能.比如我们手机有typec接口,但是我们没有这个接口的耳机,我们可以......
  • 5.1 类适配器模式
    5.1类适配器模式通过多重继承目标接口和被适配者类方式来实现适配举例(将USB接口转为VGA接口),类图如下:  USBImpl的代码:publicclassUSBImplimplementsUSB{......
  • [LeetCode] 1332. Remove Palindromic Subsequences 删除回文子序列
    Youaregivenastring s consisting only ofletters 'a' and 'b'.Inasinglestepyoucanremoveone palindromicsubsequence from s.Return the mi......
  • LQB02控制LED灯,74HC138芯片,74HC02芯片,74HC573芯片。
    一,硬件图解读。二,控制LED需要的74HC595程序编程。三,控制某个LED亮,其他保持不变,或者控制整8个LED,其他不变;四,读取某个LED的状态,一秒时间读取一次,如果是低电平,那么变为高电......
  • vue.js代码013
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><scripttype="text/javascript"src="../js/vue.js"></script></head><......
  • 「解题报告」ARC134F Flipping Coins
    简单题。但是我不会。首先考虑这个过程:我们从前往后扫,只要有正面就给翻成反面,那么最后留下的正面只有可能是当\(p_i<i\)且\(i\)没有被翻面时才会造成\(1\)的贡献......
  • 设计模式-适配器与门面模式
    1.适配器模式#include<iostream>usingnamespacestd;classThreePhasePlug{public: voiddoThreePhasePlug(){ cout<<"threephase"<<endl; }};c......
  • 13.OpenFeign测试远程调用
    以会员服务调用优惠券服务为例引入依赖在之前创建微服务模块时已经引入了这个依赖,就不需要重复引入了添加要被member微服务调用的coupon微服务的coupon的方法在memb......
  • 在 Vue 项目中使用 axios 的三种方式
    首先npmiaxios,npm下载axios插件.第一种方式:直接在vue组件中导入axios,并直接引用.注意一点,axios是一个基于promise网络请求库,这意味着,你必须使......