Reputation: 70
I'm working on an Android SDK that uses Firebase Cloud Messaging to receive push notifications from my backend. The SDK should work correctly whether the host app is also using Firebase or not. 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.
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.
In the source code for the FirebaseMessaging
class, there is a package-private initializer that takes in a 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.
What is the current recommended approach for using FCM with multiple Firebase projects in the same app?
Upvotes: 0
Views: 358
Reputation: 317467
The fact that FirebaseMessaging does not expose a public method for getInstance(app)
suggests that initialization with multiple projects are not supported for FCM. You will notice that the same is true for FirebaseAnalytics, FirebaseCrashlytics and FirebaseInAppMessaging. These products all rely on Android singleton services that must be registered in the app manifest, which can't be changed before launch. This is why they only support a single (default) FirebaseApp instance.
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.
Upvotes: 1