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

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 version of the iOS SDK before building your app with Xcode 11. All of OneSignal’s wrapper SDKs including React Native, Unity, and Flutter have been updated as well. The reason for this is that Xcode 11, which is being released alongside iOS 13, breaks a common technique that apps and libraries like OneSignal were using to get a push token for the device. If you do not use our new SDK then new users will not be able to subscribe to notifications from your app.

And I got curious about it.

This is the way we got the device notification token on iOS 12

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    var token = ""
    for i in 0..<deviceToken.count {
        token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
    }
    print("Notification token = \(token)")
}

Whats the proper way to get it on iOS 13? Should I do the new way for my currently developing apps or the old way is still fine?



Solution 1:[1]

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

Solution 2:[2]

The same code for Swift 5 but bit shorter variant. Verified at iOS 13.

func getStringFrom(token:NSData) -> String {
    return token.reduce("") { $0 + String(format: "%02.2hhx", $1) }
}

Solution 3:[3]

Correctly capture iOS 13 Device Token in Xamarin.iOS

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    //DeviceToken = Regex.Replace(deviceToken.ToString(), "[^0-9a-zA-Z]+", "");
    //Replace the above line whick worked up to iOS12 with the code below:
    byte[] bytes = deviceToken.ToArray<byte>();
    string[] hexArray = bytes.Select(b => b.ToString("x2")).ToArray();
    DeviceToken = string.Join(string.Empty, hexArray);
}

Here is what's going on here:

  1. First we have to grab all the bytes in the device token by calling the ToArray() method on it.
  2. Once we have the bytes which is an array of bytes or, byte[], we call LINQ Select which applies an inner function that takes each byte and returns a zero-padded 2 digit Hex string. C# can do this nicely using the format specifier x2. The LINQ Select function returns an IEnumerable, so it’s easy to call ToArray() to get an array of string or string[].
  3. Now just call Join() method on an array of string and we end up with a concatenated string.

Reference: https://dev.to/codeprototype/correctly-capture-ios-13-device-token-in-xamarin-1968

Solution 2: This also works fine

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    byte[] result = new byte[deviceToken.Length];
    Marshal.Copy(deviceToken.Bytes, result, 0, (int)deviceToken.Length);
    var token = BitConverter.ToString(result).Replace("-", "");
}

Solution 4:[4]

Nice solution in C# with Xamarin:

// In AppDelegate class:
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    var bytes = deviceToken.ToArray();
    var deviceTokenString = string.Concat(bytes.Select(b => $"{b:x2}"));
    // TODO: handle deviceTokenString
}

Solution 5:[5]

func getStringFrom(deviceToken: Data) -> String {
    var token = ""
    for i in 0..<deviceToken.count {
        token += String(format: "%02.2hhx", arguments: [deviceToken[i]])
    }
    return token
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 DenisKirillov
Solution 3
Solution 4 Marcel Wolterbeek
Solution 5