如何在google-services.json中放置多个project_number/sender id

How to put multiple project_number/sender id in google-services.json

我希望能够在我的 android 应用程序中添加多个发件人 ID。

来自https://developers.google.com/cloud-messaging/concept-options

GCM allows multiple parties to send messages to the same client app. For example, suppose the client app is an articles aggregator with multiple contributors, and each of them should be able to send a message when they publish a new article. This message might contain a URL so that the client app can download the article. Instead of having to centralize all sending activity in one location, GCM gives you the ability to let each of these contributors send its own messages.

这是如何使用 google-services.json 配置文件实现的?

更新:在执行此操作时将参考官方和推荐的方法,而不是 hacky 和解决 prevent/avoid 未知问题的非官方方法。根据我的回答 .

documentation里面其实有一部分是关于这个话题的:

Receiving messages from multiple senders

FCM allows multiple parties to send messages to the same client app. For example, suppose the client app is an article aggregator with multiple contributors, and each of them should be able to send a message when they publish a new article. This message might contain a URL so that the client app can download the article. Instead of having to centralize all sending activity in one location, FCM gives you the ability to let each of these contributors send its own messages.

To make this possible, make sure each sender generates its own sender ID. See the client documentation for your platform for information on on how to obtain the FCM sender ID. When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field.

Finally, share the registration token with the corresponding app servers (to complete the FCM registration client/server handshake), and they'll be able to send messages to the client app using their own authentication keys.

Note that there is limit of 100 multiple senders.

我认为这里令人困惑但重要的部分是:

When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field.

换句话说,您必须调用 getToken() 传递发件人 ID 并仅将 "FCM"(例如 getToken("2xxxxx3344", "FCM"))作为参数。您必须确保为您需要的每个 发件人(项目)调用它。

此外,getToken() 文档中的注释:

This is a blocking function so do not call it on the main thread.

一些额外的小知识:

  • 它不会像默认的那样在失败时自动重试。
  • 它在失败时 returns 一个 IOException。

截至 2016 年 12 月,有一种非常简单、非 hacky 的方法可以做到这一点,现在(2018 年 7 月)仍然有效。

FirebaseOptions options = new FirebaseOptions.Builder()
       .setApplicationId("1:something:android:something_else") // Required for Analytics.
       .setApiKey("your apikey") // Required for Auth.
       .setDatabaseUrl("https://your-database.firebaseio.com/") // Required for RTDB.
       .build();
FirebaseApp.initializeApp(this /* Context */, options, "secondary");

来源:The official Firebase blog

您可以通过将多个发件人作为逗号分隔的字符串传递给他们来获取单个令牌,然后这些发件人将能够使用公共令牌发送推送通知,请尝试调用

FirebaseInstanceId.getInstance() .getToken("senderId1,senderId2", FirebaseMessaging.INSTANCE_ID_SCOPE);

确保从后台线程调用它。

逗号分隔的 senderID 解决方案仍然有效,并且能够为 2 个不同的发件人注册相同的令牌。我使用 2 个不同的 api 密钥向那个单一的魔法令牌发送了推送通知,并且能够接收两个 api 密钥的推送通知。希望它至少能工作到 2020 年底。因为我正试图在旧的 GCM 和面向超过 100 万用户的 FCM 项目之间进行无缝过渡。 (听我说 google 并感谢 google 没有贬低这个伟大的解决方案)

String magicalToken = FirebaseInstanceId.getInstance().getToken("senderId, anotherSenderId", "FCM");