使用 Firebase 和 APNs 身份验证密钥的生产推送通知

Eli*_*tle 5 production push-notification apple-push-notifications ios firebase

在 TestFlight 上测试我的应用程序时,我注意到我没有收到推送通知。

我已经确认,当从 Xcode 加载构建时,我可以接收从云功能和 firebase 控制台发起的通知。

我参考了其他类似的问题,但没有一个反映我正在处理的确切问题。在 Firebase 中配置远程通知时,我使用的是APNs Auth Key,我还包含了.p12用于开发和生产的文件。但是,.p12由于已设置 APNs 身份验证密钥,这些文件会变灰并列为非活动状态。

在我的 Apple Developer 帐户Certificates, Identifiers & Profiles 下,我确保我的 APNs Auth Key 存在于左侧Keys部分。我还在我的 App Id 中设置了开发人员和生产 SSL 证书并验证它们是否被列为“已启用”。我还验证了我有一个“活动”的 iOS 分发配置文件。

这是一个难以调试的问题,因为 Xcode 构建可以工作,而 TestFlight 构建却不能。我知道这个问题涉及生产环境和开发环境,但我不确定如何解决这个问题。

在 Xcode 构建中调用此方法,并且正在调用所有关联的UserNotificationsMessaging委托方法。我运行iOS 10.3及已FirebaseAppDelegateProxyEnabled设置为NOinfo.plist

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
     Messaging.messaging().setAPNSToken(deviceToken, type: MessagingAPNSTokenType.sandbox)
     Messaging.messaging().setAPNSToken(deviceToken, type: MessagingAPNSTokenType.prod)
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*ris 2

对我来说,解决方法是停止使用 MessagingAPNSTokenType.sandbox 或 .prod,而只需使用:

Messaging.messaging().setAPNSToken(deviceToken, type: .unknown)
Run Code Online (Sandbox Code Playgroud)

在之前的应用程序中,我使用了:

if Config.isDebug {
    tokenType = MessagingAPNSTokenType.sandbox
} else {
    tokenType = MessagingAPNSTokenType.prod
}
Messaging.messaging().setAPNSToken(deviceToken, type: tokenType)
Run Code Online (Sandbox Code Playgroud)

并设置 Config 以依赖于 Xcode 中的构建方案。现在它似乎不适用于产品。在 Firebase 上,我现在使用APNS Key,而不是旧式的 Prod 和 Dev 证书。这可能有所作为。我看到Firebase 文档现在也说:

如果令牌类型设置为 UNKNOWN Firebase Messaging 将隐式尝试从配置文件中找出实际令牌类型。除非您确实需要指定类型,否则应该使用 APNSToken 属性。