Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the push notification token - iOS 10, Swift 3

How to get the device token from new xCode 8, Swift 3 in iOS 10?

Here is the code to register notification:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {    

    DispatchQueue.main.async {
        let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(settings)
    }
    return true
}

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {

    if notificationSettings.types != UIUserNotificationType() {
        application.registerForRemoteNotifications()
    }
}

Here i get the token, but i get the following error

Cannot invoke initializer for type 'UnsafePointer<CChar>' with an argument list of type '(UnsafeRawPointer)':

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    let chars = UnsafePointer<CChar>((deviceToken as NSData).bytes)
    var token = ""

    for i in 0..<deviceToken.count {
        token += String(format: "%02.2hhx", arguments: [chars[i]])
    }

    print("Registration succeeded!")
    print("Token: ", token)
}

Can anyone help me solve it?

like image 890
user831098 Avatar asked Sep 19 '16 04:09

user831098


People also ask

What is device token in push notifications?

It is called when the app successfully manages to register itself for push notifications. In normal conditions, the second parameter is vital, as it contains a unique key for each device, called device token. In real-world applications, you should send the device token to the server that originally sends the push notifications.

How to enable push notifications on iOS devices?

After having set the above values, scroll down until you reach the App Services area. At the bottom of the provided services, you’ll find the Push Notifications option. Click to the checkbox to mark it as selected, and make sure twice that it is checked before you continue.

What is the second parameter in push notifications?

In normal conditions, the second parameter is vital, as it contains a unique key for each device, called device token. In real-world applications, you should send the device token to the server that originally sends the push notifications.

How to notify users when something needs their attention on iOS?

Your iOS 10 application is running in the background on a user's device, and you need to notify them that something needs their attention. The best way to handle this situation is to use the local notification system to get the users attention. Plus if they miss it, it will also be logged in the notification center.


3 Answers

This method may solve your problem in iOS 10 and greater:

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(token)
}
like image 55
chirag shah Avatar answered Oct 22 '22 00:10

chirag shah


var pushToken = String(format: "%@", deviceToken as CVarArg)
pushToken = pushToken.trimmingCharacters(in: CharacterSet(charactersIn: "<>"))
pushToken = pushToken.replacingOccurrences(of: " ", with: "")
like image 24
Emre Avatar answered Oct 21 '22 23:10

Emre


Swift 3 example taken from raywenderlich.com.

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
    let tokenParts = deviceToken.map { data -> String in
        return String(format: "%02.2hhx", data)
    }  
    let token = tokenParts.joined()
    print(token)
}
like image 20
Etienne Martin Avatar answered Oct 21 '22 23:10

Etienne Martin