SwiftUI register for push notifications didRegisterForRemoteNotificationsWithDeviceToken not called

Viewed 601

I am trying to subscribe to pushes, and receive device token in didRegisterForRemoteNotificationsWithDeviceToken to send it to the server. I have a SwiftUI App which has a @UIApplicationDelegateAdaptor and it seems to work fine - didFinishLaunchingWithOptions is called normally. But neither didRegisterForRemoteNotificationsWithDeviceToken, nor didFailToRegisterForRemoteNotificationsWithError gets called. There is a message in logs "Registered for push notifications with token: <...>", which probably means registration itself succeeds. Push notifications capability is added, certificates are in place, I am testing on device, not sim, but AppDelegate's methods for pushes are just not called.

I am using xcode Version 13.2.1 and ios 15.2.

Here is how I subscribe to pushes:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, _) in
      NSLog("PUSH NOTIFICATION PERMISSION GRANTED: \(granted)")
      guard granted else { return }
      DispatchQueue.main.async {
          UIApplication.shared.registerForRemoteNotifications()
      }
}

Here are my method signatures (I know it can stop working if apple changes them a bit):

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { }
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { }

EDIT!!! My apologies, I found out the problem is AzureNotificationHubs-iOS pod being in project. I don't use it for now, but the very fact that it is there, breaks delegate method calls. Does anybody maybe know how to restore the delegate calls while still keeping the azure?

1 Answers

AzureNotificationHubs-iOS uses swizzling that is why standard delegate methods don't get called. Microsoft documentation says to "Add the NHAppDelegateForwarderEnabled key [to plist] and set the value to 0" and it works. With this key in place swizzling is not used and delegate methods work as expected.

Related