Getting Remote Notification Device Token in Swift 4?

前端 未结 8 2361
梦谈多话
梦谈多话 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:48

    Use Below code for getting Device token:

     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("Device Token = \(token)")
    
    
    
    }
    
    0 讨论(0)
  • 2021-02-20 07:49
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
        {
            let tokenChars = (deviceToken as NSData).bytes.bindMemory(to: CChar.self, capacity: deviceToken.count)
            var tokenString = ""
    
            for i in 0..<deviceToken.count {
                tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
            }
                        print("tokenString: \(tokenString)")
    }
    
    0 讨论(0)
提交回复
热议问题