Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 361
Ahmadreza Avatar asked Sep 08 '19 06:09

Ahmadreza


People also ask

Does iOS device token change?

No, Device token will always be same .

Is device token unique in iOS?

The Device Tokens are NOT unique to the phone-app pairing. They are unique to the phone only. If you have multiple apps with push on the same phone they will all use the same Device Token. The certificate you use to send the notification will dictate which app it goes to.

What is device token in Apple Push Notification?

'The device token you provide to the server is analogous to a phone number; it contains information that enables APNs to locate the device on which your client app is installed. APNs also uses it to authenticate the routing of a notification. '


2 Answers

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

like image 62
atulkhatri Avatar answered Nov 20 '22 13:11

atulkhatri


The way you do it is fine and it should continue to work on iOS 13. But some developers do it like this. To convert Data into base-16 strings, they call description, which returns something like

<124686a5 556a72ca d808f572 00c323b9 3eff9285 92445590 3225757d b83997ba> 

And then they trim < and > and remove spaces.

On iOS 13 the description called on token data returns something like

{ length = 32, bytes = 0xd3d997af 967d1f43 b405374a 13394d2f ... 28f10282 14af515f } 

Which obviously makes this way broken.

Another example of wrong implementation (already edited to include correct implementation as well).

Some more examples might be found in this thread.

like image 42
Eugene Berdnikov Avatar answered Nov 20 '22 13:11

Eugene Berdnikov