didRegisterForRemoteNotificationsWithDeviceToken is never called

Hi everyone,

Hi have no clue why I can't get my device token anymore.

Since the last Xcode update (currently 14.2), didRegisterForRemoteNotificationsWithDeviceToken is never call. It was working before.

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, TimerApplicationDelegate {

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    AppDelegate.shared = self
    // appearance
    NewCashingViewController.setupAppearance()

    // user-notification and push-notification
    registerForPushNotifications(for: application)
    return true
  }

   func registerForPushNotifications(for application: UIApplication) {
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, _ in
      print("Permission granted: \(granted)")
      guard granted else { return }
      self?.getNotificationSettings(for: application)
    }
  }

  func getNotificationSettings(for application: UIApplication) {
    UNUserNotificationCenter.current().getNotificationSettings { settings in
      print("Notification settings: \(settings)")
      guard settings.authorizationStatus == .authorized else { return }
      DispatchQueue.main.async { application.registerForRemoteNotifications() }
    }
  }

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
    let token = tokenParts.joined()
    UserDefaults.standard.set(token, forKey: UserDefaultsKeys.deviceToken)
    print("Device Token: \(token)")
  }

   func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Failed to register: \(error)")
  }
}

using Xcode 14.2, iOS 14.5 and 16.2

Thank you for your time.

Answered by WilliamCisneros in 744401022

My bad, I was using simulators, it's only work on real devices

Accepted Answer

My bad, I was using simulators, it's only work on real devices

didRegisterForRemoteNotificationsWithDeviceToken is never called
 
 
Q