[英]What is the current best practice for using Firebase Cloud Messaging with multiple Firebase projects in the same Android app?
I'm working on an Android SDK that uses Firebase Cloud Messaging to receive push notifications from my backend.我正在开发一个 Android SDK,它使用 Firebase Cloud Messaging 从我的后端接收推送通知。 The SDK should work correctly whether the host app is also using Firebase or not.
无论主机应用程序是否也在使用 Firebase,SDK 应该都能正常工作。 The FCM documentation outlines an approach for allowing multiple senders to send push notifications to the same app, however the wording is cryptic and there don't appear to be any code samples for how to do this.
FCM 文档概述了一种允许多个发件人向同一个应用程序发送推送通知的方法,但措辞含糊不清,似乎没有任何代码示例说明如何执行此操作。
After digging around, it looks like there used to be (circa 2018) a way of accomplishing this by calling FirebaseInstanceId.getInstance().getToken("senderId1,senderId2", FirebaseMessaging.INSTANCE_ID_SCOPE)
, which is now deprecated.在四处挖掘之后,看起来曾经(大约 2018 年)通过调用
FirebaseInstanceId.getInstance().getToken("senderId1,senderId2", FirebaseMessaging.INSTANCE_ID_SCOPE)
来完成此任务,现在已弃用。
In the source code for the FirebaseMessaging
class, there is a package-private initializer that takes in a FirebaseApp
object.在
FirebaseMessaging
class 的源代码中,有一个包私有初始化程序接受FirebaseApp
object。 This looks like it should be the correct way of generating FCM registration tokens for secondary Firebase apps, and indeed if I use reflection to access this initializer method and generate tokens using FirebaseMessaging.getInstance(**secondaryApp**).getToken().addOnCompleteListener(...)
I am able to send push notifications successfully, however this is a subpar solution for obvious reasons.这看起来应该是为辅助 Firebase 应用程序生成 FCM 注册令牌的正确方法,实际上,如果我使用反射来访问此初始化程序方法并使用
FirebaseMessaging.getInstance(**secondaryApp**).getToken().addOnCompleteListener(...)
我能够成功发送推送通知,但是由于显而易见的原因,这是一个低于标准的解决方案。
What is the current recommended approach for using FCM with multiple Firebase projects in the same app?在同一个应用程序中将 FCM 与多个 Firebase 项目一起使用的当前推荐方法是什么?
The fact that FirebaseMessaging does not expose a public method for getInstance(app)
suggests that initialization with multiple projects are not supported for FCM. FirebaseMessaging 没有公开
getInstance(app)
的公共方法这一事实表明,FCM 不支持使用多个项目进行初始化。 You will notice that the same is true for FirebaseAnalytics , FirebaseCrashlytics and FirebaseInAppMessaging .您会注意到FirebaseAnalytics 、 FirebaseCrashlytics和FirebaseInAppMessaging也是如此。 These products all rely on Android singleton services that must be registered in the app manifest, which can't be changed before launch.
这些产品都依赖于 Android singleton 服务,这些服务必须在应用清单中注册,在发布前无法更改。 This is why they only support a single (default) FirebaseApp instance.
这就是为什么它们只支持单个(默认) FirebaseApp实例。
You can initialize as many FirebaseApp instances you want, one for each project you want to use, but these specific products can only work with the default app.您可以初始化任意数量的 FirebaseApp 实例,为您要使用的每个项目创建一个实例,但这些特定产品只能与默认应用一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.