简体   繁体   English

接收来自两个 Firebase 项目的一个 Android 应用的推送通知

[英]Receive push notifications on one android app from two Firebase projects

I have an Android app, and I want it be able to receive push notifications from two different Firebase projects.我有一个 Android 应用程序,我希望它能够接收来自两个不同 Firebase 项目的推送通知。

I read the blog "Working with multiple Firebase projects in an Android app" https://firebase.googleblog.com/2016/12/working-with-multiple-firebase-projects-in-an-android-app.html which talks about "Accessing the Databases from two different Firebase projects".我阅读了博客“在 Android 应用程序中使用多个 Firebase 项目” https://firebase.googleblog.com/2016/12/working-with-multiple-firebase-projects-in-an-android-app.html关于“从两个不同的 Firebase 项目访问数据库”。
However, there is no info about receiving notifications from multiple Firebase projects.但是,没有关于从多个 Firebase 项目接收通知的信息。

So, how to integrate my app with multiple Firebase projects and receive push notifications from them?那么,如何将我的应用与多个 Firebase 项目集成并接收来自它们的推送通知?

There is actually a part in the documentation about this topic:关于这个主题的文档中实际上有一部分:

Receiving messages from multiple senders接收来自多个发件人的消息

FCM allows multiple parties to send messages to the same client app. FCM 允许多方向同一个客户端应用程序发送消息。 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.此消息可能包含一个 URL,以便客户端应用程序可以下载文章。 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. FCM 不必将所有发送活动集中在一个位置,而是让您能够让每个贡献者发送自己的消息。

To make this possible, make sure each sender generates its own sender ID .为了实现这一点,请确保每个发件人都生成自己的发件人 ID See the client documentation for your platform for information on on how to obtain the FCM sender ID.有关如何获取 FCM 发件人 ID 的信息,请参阅您平台的客户端文档。 When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field.请求注册时,客户端应用程序多次获取令牌,每次在受众字段中使用不同的发件人 ID。

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.最后,与相应的应用程序服务器共享注册令牌(以完成 FCM 注册客户端/服务器握手),他们将能够使用自己的身份验证密钥向客户端应用程序发送消息。

Note that there is limit of 100 multiple senders.请注意,有 100 个多个发件人的限制。

I think the confusing but important part here is:我认为这里令人困惑但重要的部分是:

When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field.请求注册时,客户端应用程序多次获取令牌,每次在受众字段中使用不同的发件人 ID。

In other terms, you'll have to call getToken() passing the Sender ID and simply "FCM" (eg getToken("2xxxxx3344", "FCM") ) as the parameters. getToken("2xxxxx3344", "FCM") ,您必须调用getToken()传递发件人 ID 和简单的"FCM" (例如getToken("2xxxxx3344", "FCM") )作为参数。 You'll have to make sure that you call this for each sender (project) that you need.您必须确保为您需要的每个发件人(项目)调用它。

Also, note from the getToken() docs:另外,请注意getToken()文档:

This is a blocking function so do not call it on the main thread.这是一个阻塞函数,所以不要在主线程上调用它。

Some additional good-to-knows:一些额外的注意事项:

  • It does not auto retry if it fails like the default one.如果它像默认的一样失败,它不会自动重试。
  • It returns an IOException when it fails.它在失败时返回 IOException。

I had some issues when implementing the accepted answer and hence went ahead and tried to do it on my own when I came to find a robust solution.我在实施接受的答案时遇到了一些问题,因此当我找到一个强大的解决方案时,我继续尝试自己做。 I have shared in detail there solution here .我在这里详细分享了解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android- App无法从解析平台接收推送通知 - Android- App does not receive push notifications from parse platform 如何使用Firebase从Android应用程序发送推送通知? - How to send push notifications FROM an android app with Firebase? Android-所有Android版本均可接收Firebase推送通知 - Android - Can all Android versions receive Firebase push notifications 在 Android 应用程序中未收到 Firebase 云推送通知 - Not receiving Firebase cloud push notifications in Android app Firebase Cloud Messaging Android接收静默推送通知 - Firebase Cloud Messaging Android receive silent push notifications Android Studio:应用程序未从parse.com接收推送通知 - Android Studio: App does not receive push notifications from parse.com 应用程式被杀死时,Android无法接收到解析推送通知 - Unable tor receive parse push notifications when app is killed Android 为什么我在 firebase android 通知中只收到一行文字? - Why I receive just one line of text in firebase android notifications? 使用 FCM 在 Android 应用程序中接收来自多个发件人(Firebase 项目)的消息 - Receive messages from multiple senders (Firebase projects) in Android app using FCM Android收到静音的Firebase通知 - Android Receive Silent Firebase Notifications
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM