未调用 SwiftUI 注册推送通知 didRegisterForRemoteNotificationsWithDeviceToken

SwiftUI register for push notifications didRegisterForRemoteNotificationsWithDeviceToken not called

我正在尝试订阅推送,并在 didRegisterForRemoteNotificationsWithDeviceToken 中接收设备令牌以将其发送到服务器。我有一个带有 @UIApplicationDelegateAdaptor 的 SwiftUI 应用程序,它似乎工作正常 - didFinishLaunchingWithOptions 被正常调用。但是 didRegisterForRemoteNotificationsWithDeviceTokendidFailToRegisterForRemoteNotificationsWithError 都没有被调用。日志中有一条消息“Registered for push notifications with token: <...>”,这可能意味着注册本身成功了。添加了推送通知功能,证书到位,我正在设备上测试,而不是 sim,但只是没有调用 AppDelegate 的推送方法。

我正在使用 xcode 版本 13.2.1 和 ios 15.2。

这是我订阅推送的方式:

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()
      }
}

这是我的方法签名(我知道如果 apple 稍作更改它可能会停止工作):

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

编辑!!!抱歉,我发现问题是 AzureNotificationHubs-iOS pod 在项目中。我现在不使用它,但事实上它在那里,中断了委托方法调用。有人可能知道如何在保持 azure 的同时恢复委托调用吗?

AzureNotificationHubs-iOS 使用 swizzling,这就是不调用标准委托方法的原因。 Microsoft 文档说“添加 NHAppDelegateForwarderEnabled 键 [到 plist] 并将值设置为 0”并且它有效。有了这个关键,就不会使用调配,委托方法会按预期工作。