为什么我不能在swift5中从模拟器获得设备令牌?

时间:2019-09-24 05:06:18

标签: ios swift token device

我正在迅速开发iOS。我的问题是我无法从模拟器获得设备令牌值。这正常吗?我必须做一个推送测试。推送测试只能在物理设备上进行吗?

AppDelegate

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.main.bounds)
        // Override point for customization after application launch.
        //create the notificationCenter
       let center  = UNUserNotificationCenter.current()
       center.delegate = self
        // set the type as sound or badge
       center.requestAuthorization(options: [.sound,.alert,.badge]) { (granted, error) in
            // Enable or disable features based on authorization
        }
        application.registerForRemoteNotifications()
        return true
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let token = deviceToken.map{ String(format: "%02x", $0) }.joined()
        Log.Info("Registration succeeded!")
        Log.Info("Token: \(token)")
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        Log.Warning("Registration failed!")
    }

2 个答案:

答案 0 :(得分:0)

首先,您无法在模拟器上测试推送通知。为了测试推送通知,您需要物理设备。

模拟器不提供设备令牌。只有物理设备在通知的委托方法中提供设备令牌。

答案 1 :(得分:0)

正如@SPatel所说,它只能在真实设备上进行测试。我希望将来会改善