Getting Remote Notification Device Token in Swift 4?

前端 未结 8 2360
梦谈多话
梦谈多话 2021-02-20 06:59

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 1         


        
相关标签:
8条回答
  • 2021-02-20 07:24

    You can use this both function for getting Device Token & FCM Token:


    For device token use this:

    func application(_ application:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken 
    devicetoken: Data)
     {
            let deviceTokenString = deviceToken.hexString
            print(deviceTokenString)
     }
    

    For FCM token use this:

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        userDefault.set(fcmToken, forKey: "fcmToken")
    
    }
    

    For UUID use this:

     let deviceIds = UIDevice.current.identifierForVendor!.uuidString
    
    0 讨论(0)
  • 2021-02-20 07:26

    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)
    }
    
    0 讨论(0)
  • 2021-02-20 07:30

    Swift 5

    You can use like,

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let hexString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print(hexString)
    }
    
    0 讨论(0)
  • 2021-02-20 07:31

    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 notificationTypes: UIUserNotificationType = [UIUserNotificationType.alert,UIUserNotificationType.badge, UIUserNotificationType.sound]
        let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
        
        application.registerUserNotificationSettings(pushNotificationSettings)
        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)")
    }
    
    0 讨论(0)
  • 2021-02-20 07:40

    To get device token

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // Convert token to string
        let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    }
    

    Hope this will help you

    0 讨论(0)
  • 2021-02-20 07:40

    You can get the device token like this:

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let token = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
        print("Device Token : ",token)
    }
    
    0 讨论(0)
提交回复
热议问题