0

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?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I have reached out to the Firebase devs to see what their recommended approach is. Using the package-private constructor of `FirebaseMessaging` to initialize it with my secondary app and generate FCM registration tokens does _not_ appear to affect FCM functionality in 3rd party apps as far as my local testing has revealed. I am speculating that there _is_ some behavior that may be incorrect as a result, however for the limited purpose of generating these tokens it does not seem to be an issue. I'll update with an answer if anything comes up. – Thuy Guevarra Dec 28 '20 at 03:21

1 Answers1

1

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks for your answer Doug, what do you make of this blurb in the documentation about [Receiving messages from multiple senders](https://firebase.google.com/docs/cloud-messaging/concept-options#receiving-messages-from-multiple-senders)? It seems like this use case is intended to be supported, but I haven't figured out what they're suggesting to do here. – Thuy Guevarra Dec 27 '20 at 19:19
  • 1
    sending from multiple parties != receiving from multiple projects – Doug Stevenson Dec 27 '20 at 19:20
  • I understand what you're saying, but these lines in particular that have me puzzled: ```Make sure that you do not add multiple sender IDs to a single token request, as this can have unpredictable results. Make each call separately, once per sender ID. Finally, share the registration token with the corresponding senders, and they'll be able to send messages to the client app using their own authentication keys.``` I see no way to make multiple calls to `getToken()` with different inputs, and it does sound like this is trying to explain how independent parties could target your app – Thuy Guevarra Dec 27 '20 at 19:28
  • If you have questions or feedback about the clarity of the documentation, I would encourage you to use the "send feedback" button at the top of the page. You could also [contact Firebase support directly](https://support.google.com/firebase/contact/support). But again, parties != projects. If they were the same, I would expect the author to use the same term consistently. – Doug Stevenson Dec 27 '20 at 19:31