为什么我无法从 swift5 中的模拟器获取设备令牌?

Why can't I get a device token from the simulator in swift5?

我正在用 swift 开发 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", [=10=]) }.joined()
        Log.Info("Registration succeeded!")
        Log.Info("Token: \(token)")
    }

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

首先你不能在模拟器上测试推送通知。要测试推送通知,您需要物理设备。

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

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