我怎样才能在swift 4中获得deviceToken?

时间:2017-11-20 06:31:19

标签: ios swift apple-push-notifications devicetoken

我正在使用此代码获取设备令牌它在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("", {$0 + String(format: "%02X", $1)})
    print(deviceTokenString)
}

8 个答案:

答案 0 :(得分:11)

假设您已根据代码检查了所有设置是否正确,它似乎应该可以正常工作,您只需将格式更改为%02.2hhx而不是{{1}获取适当的十六进制字符串。因此,你应该得到一个有效的。

作为一种良好做法,您可以将data extension添加到项目中以获取字符串:

%02X

用法:

import Foundation

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

答案 1 :(得分:4)

在iOS 11或更高版本,Swift 4中获取deviceToken的工作代码

请求用户权限

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        
}

获取设备令牌

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

    let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
    print(token) 
}

如果发生错误

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

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

答案 2 :(得分:0)

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)")
}

答案 3 :(得分:0)

获取设备令牌

0=> array(
    0=>     ip vrf Access-Mgmt
        rd 65000:3298
        route-target export 65000:4585
        route-target export 65000:4717
        route-target import 65000:4585
        route-target import 65000:2659
)

希望这会对你有所帮助

答案 4 :(得分:0)

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

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let token = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print("Device Token : ",token)
}

答案 5 :(得分:0)

使用以下代码获取设备令牌:

 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)")



}

答案 6 :(得分:0)

雨燕5

您可以使用like

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

答案 7 :(得分:0)

您可以同时使用这两个功能来获取设备令牌和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