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?
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.
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.
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.
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.
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)
}
var pushToken = String(format: "%@", deviceToken as CVarArg)
pushToken = pushToken.trimmingCharacters(in: CharacterSet(charactersIn: "<>"))
pushToken = pushToken.replacingOccurrences(of: " ", with: "")
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With