ios - FIrebase FCM - 推送消息最终出现在错误的应用程序中

标签 ios firebase firebase-cloud-messaging

我正在使用 Firebase Cloud Messaging 向许多 iOS 应用程序发送推送消息。 我的 FCM 设置包含一个 Firebase 项目和多个 Firebase 应用程序:

FB Project
  App1 Android
  App1 iOS
  App2 Android
  App2 iOS
  ...

现在的问题是,发送到我的一个 iOS 应用程序的 FCM 消息最终被 另一个 iOS 应用程序接收(发送到 App1 iOS - App2 iOS 获取消息)。

为了调试这个问题,我遵循了这个优秀的调试指南: https://firebase.googleblog.com/2017/01/debugging-firebase-cloud-messaging-on.html

我发现:

  1. 直接通过 APNS 传递消息效果很好(参见第 4 节)
  2. 使用 cURL 通过 FCM 传递消息(第 5 部分)将消息发送到随机应用。

然后我意识到我设备上的两个客户端应用程序都具有相同的 firebase 设备 token 。所以 Firebase 似乎不会在 token 级别区分应用程序。
我仍然希望我的 iOS 的 所有 都能收到这条消息,而不是随机收到一条消息。

问题 1:这是 Firebase 的预期行为吗?

现在在真实的应用程序中,我通过注册到不同的 channel 来定位不同的应用程序,并在应用程序标识符前加上这样的前缀:

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [[FIRMessaging messaging] subscribeToTopic:@"/topics/app1-news"]]; 
}

发送到该 channel 会产生随机结果,包括:

  • 预期的 (App1) 收到的消息
  • 另一个应用程序(例如 App2)收到的消息
  • 多个应用程序(App1、App2、...)收到的消息
  • 一个应用程序(App2,App2)多次收到消息
  • 根本没有收到消息

问题 2:我怎样才能做到这一点?

最佳答案

这不是预期的行为。 FCM token 与授权实体(整个 firebase 项目相同)和范围(通常为“*”)一起存储在钥匙串(keychain)中。但是,钥匙串(keychain)根据应用程序的包标识符存储它们,应该将它们彼此分开。

健全性检查:您是否为每个 iOS 应用使用了不同的 GoogleService-Info.plist 文件?

此外,您是否使用共享钥匙串(keychain)访问组?共享钥匙串(keychain)访问组是否位于您权利中 keychain-access-groups 列表的顶部?这可以解释 SDK 如何意外找到其他应用程序的 FCM token 。在documentation for Keychain :

When your app creates a keychain item, if you do not explicitly specify the kSecAttrAccessGroup key in the item’s attributes dictionary, Keychain Services uses the first group of the app’s access groups array (ordered as shown above) as the default access group. If your app has a keychain-access-groups entitlement, Keychain Services uses the first of these. Otherwise, it uses the application identifier, which is always present. Thus, by default, unless you add a keychain-access-groups entitlement, an app creates keychain items to which only it has access.

SDK 通过尝试创建一个小的钥匙串(keychain)项并查看访问组来确定“默认访问组”(keychain-access-groups 列表中的第一个是默认值)。通常对于钥匙串(keychain)访问组,您希望将应用程序的应用程序标识符作为第一项,然后是任何共享组。这样,除非另有说明,否则您的钥匙串(keychain)项将写入单个应用程序,除非明确存储在共享访问组中。

关于ios - FIrebase FCM - 推送消息最终出现在错误的应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45768939/

相关文章:

ruby-on-rails - 我正在使用 `fcm` gem 发送推送通知,它适用于 Android,但不适用于 IOS

iphone - 根据 iOS 版本有条件地包含 header

ios - 我什么时候需要使用 CKRecord 的 encodeSystemFieldsWithCoder?

javascript - Cordova Firebase 身份验证步骤 7 "universal-links "

firebase - Flutter:cloud_firestore 的构建错误:高于版本 "0.7.4"

firebase - 没有应用服务器的网络上的 FCM 是否安全?

ios - 如何使用自动布局根据其 subview 调整父 View 的高度

ios - 如何使用 swift 在 Xcode 中从一个 TableView 切换到另一个 TableView

android - 如何在 Android 中暂停用户帐户?

firebase - cordova-plugin-fcm - 未定义 FCMPlugin