How to convert an APNS token to String from Data in swift

 Device token convert to String but its show on data formate.

How to convert an APNS token to String from Data

 //Remote notification Registration callback methods
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
      //Call only if MoEngageAppDelegateProxyEnabled is NO
        MoEngage.sharedInstance().setPushToken(deviceToken)
        print(deviceToken)
        let token = String(deviceToken: deviceToken)
        print(token)
    }

Best way to convert an APNs token to String from Data.

// Define initializer convert an APNs token to String from Data-----
extension String {
    public init(deviceToken: Data) {
        self = deviceToken.map { String(format: "%.2hhx", $0) }.joined()
    }
}

Device token is of 32 Bytes. Written in hexadecimal they will take 64 digits.
Sample device token -

12b8463c1650fd2616937b913eff2fd7c84ffbc2ea741c7130374e492c6f5ad9

Thanks for reading!!

Similar solutions you may also like...

Post a Comment

0 Comments