Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Remote Notification Device Token in Swift 4?

I am using this code to get the Notification Center Device token.

It was working in Swift 3 but not working in Swift 4. What changed?

if #available(iOS 10.0, *) {
    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in

    }
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {    
    let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print(deviceTokenString)
}
like image 230
Sandip Gill Avatar asked Nov 20 '17 06:11

Sandip Gill


People also ask

Where do I find my iPhone device token?

Install either Xcode or iPhone Configuration UtilityHighlight your device name on the left, and go to the Console tab. Copy the above, remove spaces if present. This is your device token.

How is device token generated in iOS?

Apple Push Notification service (APNs) must know the address of a user's device before it can send notifications to that device. This address takes the form of a device token unique to both the device and your app.


2 Answers

Assuming that you already checked that everything has been setup right, based on your code, it seems that it should works fine, all you have to do is to change the format to %02.2hhx instead of %02X to get the appropriate hex string. Thus you should get a valid one.

As a good practice, you could add a Data extension into your project for getting the string:

import Foundation

extension Data {
    var hexString: String {
        let hexString = map { String(format: "%02.2hhx", $0) }.joined()
        return hexString
    }
}

Usage:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let deviceTokenString = deviceToken.hexString
    print(deviceTokenString)
}
like image 178
Ahmad F Avatar answered Nov 03 '22 12:11

Ahmad F


Working Code for getting deviceToken in - iOS 11 or greater,

Swift 4 | Swift 5

Request user permission

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in

        // If granted comes true you can enabled features based on authorization.
        guard granted else { return }

        application.registerForRemoteNotifications()
    }        
    return true        
}

Getting device token

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
    let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
    print(token) 
}

In case of error

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

    print("i am not available in simulator :( \(error)")
}
like image 23
Nikunj Kumbhani Avatar answered Nov 03 '22 12:11

Nikunj Kumbhani