14

I have an Android app, and I want it be able to receive push notifications from two different Firebase projects.

I read the blog "Working with multiple Firebase projects in an Android app" https://firebase.googleblog.com/2016/12/working-with-multiple-firebase-projects-in-an-android-app.html which talks about "Accessing the Databases from two different Firebase projects".
However, there is no info about receiving notifications from multiple Firebase projects.

So, how to integrate my app with multiple Firebase projects and receive push notifications from them?

jihad naim
  • 143
  • 1
  • 6

2 Answers2

11

There is actually a part in the documentation about this topic:

Receiving messages from multiple senders

FCM allows multiple parties to send messages to the same client app. For example, suppose the client app is an article aggregator with multiple contributors, and each of them should be able to send a message when they publish a new article. This message might contain a URL so that the client app can download the article. Instead of having to centralize all sending activity in one location, FCM gives you the ability to let each of these contributors send its own messages.

To make this possible, make sure each sender generates its own sender ID. See the client documentation for your platform for information on on how to obtain the FCM sender ID. When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field.

Finally, share the registration token with the corresponding app servers (to complete the FCM registration client/server handshake), and they'll be able to send messages to the client app using their own authentication keys.

Note that there is limit of 100 multiple senders.

I think the confusing but important part here is:

When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field.

In other terms, you'll have to call getToken() passing the Sender ID and simply "FCM" (e.g. getToken("2xxxxx3344", "FCM")) as the parameters. You'll have to make sure that you call this for each sender (project) that you need.

Also, note from the getToken() docs:

This is a blocking function so do not call it on the main thread.

Some additional good-to-knows:

  • It does not auto retry if it fails like the default one.
  • It returns an IOException when it fails.
Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • It looks like to me it has overridden the config that I wanted to use primarily for everything. That's not what I want to achieve. – mrpasqal Jul 24 '18 at 12:26
  • @AL. This has to be the approach when the callback [onNewToken(String token)](https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService.html#onNewToken(java.lang.String)) is invoked, rt? I believe this question is related to the original question for complete implementation.The same is mentioned clearly [here](https://developers.google.com/instance-id/guides/android-implementation#refresh_tokens) with sample code in case of GCM. But, it is not clearly mentioned in any official docs for FCM. Do you have any official link which answers my question? – garnet Sep 11 '18 at 14:39
  • @garnet `onNewToken()` is the [new counterpart for `onTokenRefresh()`](https://stackoverflow.com/q/51296171/4625829) and is different from `getToken()`. – AL. Sep 11 '18 at 17:02
  • @AL., Yes, I know that. My concern is that to completely answer the question asked, answer should explain how to handle [onNewToken(String token)](https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService.html#onNewToken(java.lang.String)) or onTokenRefresh as you mentioned. Please refer my previous comment regarding official link with sample code in case of GCM. – garnet Sep 12 '18 at 10:45
  • @garnet I'm not sure what kind of sample you are looking for. `onNewToken()` triggers when it detects that a newer version of the token for the corresponding device is available. What to do with the returned token is up to you. Usually, it is being sent to the backend server to replace the old one. – AL. Sep 12 '18 at 16:57
  • @AL. To be more clear regarding your statement, `onNewToken(String token)`is 'Called when a new token for the default Firebase project is generated'. When `onNewToken(String token)` is called, I think if app has multiple senderId use-case, it is supposed to call getToken(senderId, "FCM") multiple times, each time with a different sender ID. This was the case with `onTokenRefresh` as explained with [example](https://developers.google.com/instance-id/guides/android-implementation#refresh_tokens). But, I could not find this in FCM documentation. Do you have any link which explains the same? – garnet Sep 18 '18 at 08:12
  • 1
    @garnet The example link you provided is the exact documentation available. The InstanceId service is the same service used by the FirebaseInstanceId service. Cheers! – AL. Sep 18 '18 at 08:57
  • @ AL , @garnet , Regarding "onNewToken (String token)", documentation says **"Called when a new token for the default Firebase project is generated. This is invoked after app install when a token is first generated, and again if the token changes."** What if application is receiving push notification from multiple sender id?if onNewToken is called for default project only then how application will know in case if other tokens(retrieverd from other senderId) needs to be refreshed? onTokenRefresh is called whenever any of token associated with perticulat instance id need to be refreshed. – Hey You Sep 22 '18 at 12:17
  • Due to the character limit, adding the reference of my original question [here](https://stackoverflow.com/questions/52328630/how-to-determine-if-token-needs-to-be-refreshed-in-case-of-multiple-sender-id) – Hey You Sep 22 '18 at 12:22
  • Is it means that given a sender id, we can then send msg to the any server? If the answer is yes, I thank that's a severe security problem on it. If not, how it works? (I tested in my app, given a sender id, I can send msg to our server) – Jekton Apr 12 '19 at 04:10
2

I had some issues when implementing the accepted answer and hence went ahead and tried to do it on my own when I came to find a robust solution. I have shared in detail there solution here.

Tejas Sherdiwala
  • 750
  • 8
  • 15