也可以通过重新连接wifi或者切换wifi和蜂窝数据来解决上述问题。
此外,iOS 13 中的更改影响了推送通知的实现。
在 iOS 13 之前,我们很多人都曾经这样做过
(deviceToken as NSData).description
// Used to return as follows
"<965b251c 6cb1926d e3cb366f dfb16ddd e6b9086a 8a3cac9e 5f857679 376eab7C>"
let tokenData = deviceToken as NSData
let token = tokenData.description
let token = "\(deviceToken)".replacingOccurrences(of: " ", with: "")
.replacingOccurrences(of: "<", with: "")
.replacingOccurrences(of: ">", with: "")
在 iOS 13 中,苹果更改了 NSData 类的描述方法的实现。
所以,它返回
"{length = 32, bytes = 0x965b251c 6cb1926d e3cb366f dfb16ddd ... 5f857679 376eab7c }" // in iOS 13.
这最终破坏了许多应用程序的推送通知实现。
从现在开始,如果您需要将推送通知注册 deviceToken 转换为 Base16 编码/十六进制字符串表示形式,您应该针对 Swift 语言执行以下操作
let deviceTokenString = deviceToken.map { String(format: "%02x", $0)
}.joined()
对于 Objective C,使用以下代码
- (NSString *)hexadecimalStringFromData:(NSData *)deviceToken {
NSUInteger dataLength = deviceToken.length;
if (dataLength == 0) {
return nil;
}
const unsigned char *dataBuffer = (const unsigned char *)deviceToken.bytes;
NSMutableString *hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
for (NSInteger index = 0; index < dataLength; ++index) {
[hexString appendFormat:@"%02x", dataBuffer[index]];
}
return [hexString copy];
}
我发现了一篇关于给定主题的综合文章https://nshipster.com/apns-device-tokens/ https://nshipster.com/apns-device-tokens/