Search code examples
iosswifttokendevice

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


I am developing iOS with swift. My problem is that I can't get a device token value from the simulator. Is this normal? I have to do a push test. Is push testing possible only on the physical device?

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

Solution

  • As @SPatel said, it could only be tested on a real device. I hope it will improve in the future