在 Swift 4 中获取远程通知设备令牌?

Getting Remote Notification Device Token in Swift 4?

我正在使用此代码获取通知中心设备令牌。

它在 Swift 3 中工作但在 Swift 4 中不工作。发生了什么变化?

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("", {[=11=] + String(format: "%02X", )})
    print(deviceTokenString)
}

假设您已经检查过一切都已正确设置,根据您的代码,它似乎应该可以正常工作,您所要做的就是将格式更改为 %02.2hhx 而不是 %02X 以获得适当的十六进制字符串。因此你应该得到一个有效的。

作为一种好的做法,您可以在项目中添加一个 Data 扩展以获取字符串:

import Foundation

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

用法:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let deviceTokenString = deviceToken.hexString
    print(deviceTokenString)
}
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)")
}

获取设备令牌

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

希望对您有所帮助

获取 deviceToken 的工作代码 - iOS 11 或更高,

Swift 4 | Swift 5

请求用户许可

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        
}

正在获取设备令牌

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

万一出错

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

    print("i am not available in simulator :( \(error)")
}

您可以这样获取设备令牌:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let token = deviceToken.reduce("", {[=10=] + String(format: "%02X", )})
    print("Device Token : ",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)")



}

Swift 5

你可以用赞,

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let hexString = deviceToken.map { String(format: "%02.2hhx", [=10=]) }.joined()
    print(hexString)
}

您可以使用这两个函数来获取设备令牌和 FCM 令牌:


对于设备令牌使用此:

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

对于 FCM 令牌,使用此:

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

}

对于 UUID 使用此:

 let deviceIds = UIDevice.current.identifierForVendor!.uuidString