Allow FirebaseMessaging to be initialized with a secondary FirebaseApp
See original GitHub issueWhat feature would you like to see?
The Firebase Cloud Messaging 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)
. This would allow for the sender id of the secondary Firebase project to be specified when the token is created, however FirebaseInstanceId
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.
It’d be great to support this method for public use, or at least have more clarity on what the current recommended approach for using FCM with multiple projects in the same app is.
How would you use it?
I am developing an SDK that uses FCM to receive data push notifications from my backend. The SDK should function correctly whether the host app is also using Firebase or not. Currently, when the host app is using Firebase FirebaseMessaging
picks up on the initialization parameters of the host app instead of my SDK. When I attempt to send push notifications from my server to these FCM registration tokens, I get SenderId mismatch
errors (as I would expect if the tokens belong to a different project). If I use reflection to instantiate FirebaseMessaging
with my SDK’s FirebaseApp
or if the host app is not using Firebase, I am able to send push notifications successfully.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:7 (1 by maintainers)
Top GitHub Comments
I have verified a way to do it as I had been facing a similar issue.
I registered one of the projects using the
google-services.json
file.Now as per the documentation :
Here the word “default” is of key importance. It mentions that the
onNewToken
method in the overriddenFirebaseMessagingService
(eg: MyFirebaseMessagingService) will only be called for the default project.Hence in this case the first project configured using the
google-services.json
will be the default project and for that theonNewToken
method will be called.For the second project, I manually configured the project using the code below following the documentation:
The values for the parameters can be obtained from the
google-services.json
file of the second project. (NOTE: DON’T INCLUDE THE SECOND PROJECT’Sgoogle-services.json
IN THE PROJECT)google-services.json
to manual code mappingproject_id
key in the root of the jsonclient > client_info > mobilesdk_app_id
. Incase of multiple project make sure that the client used is of thepackage_name
which matches the Android appclient > api_key > current_key
(make sure of the package name here as well.KEY CODE
The most important part here which is tough to find in the documentation is to get the token of the second firebase project.
Your code works as expected, however the FirebaseApp#get method is annotated as @KeepForSdk.
This is the reason why this api not appear in official javadoc (https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/FirebaseApp)
Here a clarification from the Firebase SDK team i think is required.
If the latest versions of firebase SDK are not able to support multiple sender id this is a point tha should be marked to make more aware the developers in case of product / project migrations
@cpsloal : What do you think about it?