Does iOS 13 has new way of getting device notification token?

后端 未结 7 1653
自闭症患者
自闭症患者 2020-12-03 04:55

So my friend got this email from OneSignal

Due to a change that may occur as part of the upcoming iOS 13 release, you must update to the latest versio

相关标签:
7条回答
  • 2020-12-03 05:30

    You may use this method to fetch the device token on iOS 13 onwards:

    Objective-C:

    + (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];
    }
    

    Swift 5.0 (Untested)

    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
    }
    

    Taken from OneSignal blog

    0 讨论(0)
提交回复
热议问题