I have a Swift application with push notifications. Whenever the device registered a device token to receive push notifications I got a normal string like: QY1WcHzcxSbAYPJa0OrTQwFhYZ3ilRUQ0HmKgUlP4IBsLQAJazZnZ4XuGrNO2l4S
. However, with iOS 13 a new device token is received in the format: { length = 32, bytes = 0xd3d997af 967d1f43 b405374a 13394d2f ... 28f10282 14af515f }
.
I use the following code to convert the new device token:
let deviceTokenString = deviceToken.map { String(format: "%02x", $0) }.joined()
This gives me back a string with 64 characters.
The original application is not build in swift. How can I make an implementation in javascript/php/typescript where I receive a string like: { length = 32, bytes = 0xd3d997af 967d1f43 b405374a 13394d2f ... 28f10282 14af515f }
and convert it to: QY1WcHzcxSbAYPJa0OrTQwFhYZ3ilRUQ0HmKgUlP4IBsLQAJazZnZ4XuGrNO2l4S
For Swift 5.0 you need to use below code.
class func string(fromDeviceToken deviceToken: Data?) -> String? {
let length = deviceToken?.count ?? 0
if length == 0 {
return nil
}
let buffer = UInt8(deviceToken?.bytes ?? 0)
var hexString = String(repeating: "\0", count: length * 2)
for i in 0..<length {
hexString += String(format: "%02x", buffer[i])
}
return hexString
}
And if you are using Objective-C then You need to use below code:
+ (NSString *)stringFromDeviceToken:(NSData *)deviceToken {
NSUInteger length = deviceToken.length;
if (length == 0) {
return nil;
}
const unsigned char *buffer = deviceToken.bytes;
NSMutableString *hexString = [NSMutableString stringWithCapacity:(length * 2)];
for (int i = 0; i < length; ++i) {
[hexString appendFormat:@"%02x", buffer[i]];
}
return [hexString copy];
}
You can check this out Apple Push Notification Forums
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.