firebase / firebase/firebase-android-sdk

Allow FirebaseMessaging to be initialized with a secondary FirebaseApp

Open
#2,285 8 comments 15 reactions 1 assignee Claimed by @cpsloal View on GitHub
api: messaging type: feature request
Dominant language
Java
Stars
2.6k
Forks
710
Avg merge
2d 9h
Merged PRs (30d)
31

Description

## What feature would you like to see?

The Firebase Cloud Messaging documentation [outlines](https://firebase.google.com/docs/cloud-messaging/concept-options#receiving-messages-from-multiple-senders) 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](https://stackoverflow.com/questions/37974658/how-to-put-multiple-project-number-sender-id-in-google-services-json) (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](https://github.com/firebase/firebase-android-sdk/blob/e65f4876ff61b5791986c249b21cb094c37bd29e/firebase-messaging/src/main/java/com/google/firebase/messaging/FirebaseMessaging.java#L88) 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.