首页 > 解决方案 > didRegisterForRemoteNotificationsWithDeviceToken 未调用 Xcode 12.5

问题描述

我正在尝试通过 Firebase 在我的应用中实现推送通知。

我已按照此处的说明进行操作:https ://firebase.google.com/docs/cloud-messaging/ios/first-message#register_for_remote_notifications ,并授予了权限。我得到一个 FCM 令牌,但测试消息不起作用。

我找到了一个调试指南,建议我手动调用这些方法,但didRegisterForRemoteNotificationsWithDeviceToken或者didFailToRegisterForRemoteNotificationsWithError永远不会被调用。

显然,我认为这是一个代码或配置问题,但我已经尝试将它们排除在外,但没有这样的运气。当我在这里下载 iOS 示例应用程序时:https ://github.com/firebase/quickstart-ios/tree/master/messaging/

一切都很好。我没有更改任何代码,只更改了包 ID 和 GoogleServices-Info.plist。我收到了一个 APNs 返回,同样的推送效果很好。

因此,我将整个 AppDelegate.swift 复制到我的项目中。相同的代码,相同的 App Id,启用推送通知,自动管理签名。它不起作用,didRegisterForRemoteNotificationsWithDeviceToken没有被调用。

我能找到的唯一区别如下: • 示例应用程序没有使用 SwiftUI。我正在使用 UIApplicationDelegateAdaptor 转发 AppDelegate,并且可以很好地调用 didFinishLaunchingWithOptions • 我使用的是 Swift 包管理器而不是 CocoaPods。代码编译良好,没有错误消息。

以下是我所知道的相同: • 匹配应用程序代表。复制/粘贴。应用中没有与通知相关的其他代码 • 匹配应用功能 • 匹配应用 ID、GoogleServiceInfo.plist • 匹配“自动管理签名”

在这一点上,我没有想法。任何其他要检查的事情的建议都会很棒。什么会导致didRegisterForRemoteNotificationsWithDeviceToken不被调用?

标签: iosswiftfirebasepush-notificationapple-push-notifications

解决方案


I solved the problem. It ended up being that a SwiftUI app did not support method swizzling by default. Instead, Firebase was trying to intercept the method unsuccessfully, resulting in the original method not being called

Disabling swizzling with the recommended flag and then sending it manually fixed the issue.


推荐阅读