8

I want to be able to add more than one sender id in my android app.

From https://developers.google.com/cloud-messaging/concept-options

GCM allows multiple parties to send messages to the same client app. For example, suppose the client app is an articles 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, GCM gives you the ability to let each of these contributors send its own messages.

How is this achieved using google-services.json configuration file?

AL.
  • 36,815
  • 10
  • 142
  • 281
Zyoo
  • 773
  • 10
  • 30
  • I'll reply to your comment here. Since long comment chains would get your post deleted. Go ahead. FCM is easy to setup. Cheers! – AL. Jun 23 '16 at 04:08
  • How was it @Zyoo? Were you able to tey it out? :) – AL. Jun 23 '16 at 11:26
  • @intj I tried your method, but got exception `06-23 21:24:07.009 7149-8358/com.google.firebase.quickstart.fcm D/FirebaseInstanceId: background sync failed: INVALID_SENDER, retry in 80s` `06-23 21:24:51.959 7149-7149/com.google.firebase.quickstart.fcm D/MainActivity: InstanceID token: null` – Zyoo Jun 23 '16 at 14:30
  • 1
    Got it working, there was a space in here `"project_number": "487xxx, 102xxx"` but after removing it, the token is not null. I'll accept your answer and write some addition (using GCM) after this. – Zyoo Jun 23 '16 at 14:34
  • That's great. :) Good question btw. Cheers! :D – AL. Jun 23 '16 at 20:08
  • Hi. Was just going through some old GCM posts and was hoping if you can accept my answer or put in another answer that resolved your post, so that it'll be properly tagged as already answered. Cheers! – AL. Dec 07 '16 at 10:50

4 Answers4

7

UPDATE: Going to refer to the official and recommended way in doing this instead of the hacky and unofficial approach to prevent/avoid unknown problems. From my answer here.

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
  • So, this is done using a single registration token? Do you mind sharing the code where you request the token? – Zyoo Jun 23 '16 at 03:30
  • Yup. Single registrationToken. I just used the code from the quickstart I linked. – AL. Jun 23 '16 at 03:31
  • How do you send the notification? I got failure "mismatched sender id" – Zyoo Jun 23 '16 at 03:40
  • I sent one through the [Firebase Console](https://console.firebase.google.com). Under the Notification, then New Message. – AL. Jun 23 '16 at 03:42
  • Okay, I looked at firebase docs, there was no mention of any `google-services.json`. It looks like firebase doesn's use this file – Zyoo Jun 23 '16 at 03:53
  • Ahmm.. They do. Here's a [screenshot](http://i.stack.imgur.com/jE3oP.png) of my Console. – AL. Jun 23 '16 at 04:01
  • 1
    It seems that I have to use FCM. I'll try to do that now – Zyoo Jun 23 '16 at 04:05
  • You saved my day. I tried many things and none worked. This one is returning a token that I can still use with my old project. Thanks – Roberto Oct 21 '16 at 10:11
  • 1
    @Lancelot You're welcome. Do note that this is just a hacky approach and that it is not advisable to modify the `google-services.json` file. Cheers! – AL. Oct 21 '16 at 12:51
  • For future readers, my answer [here](http://stackoverflow.com/a/42118836/4625829) explains how you set up multiple senders for a single client app by calling `getToken()`. Cheers! :) – AL. Feb 09 '17 at 10:08
1

As of Dec. 2016, there's a very simple, non-hacky way to do this, which still works now (Jul 2018).

FirebaseOptions options = new FirebaseOptions.Builder()
       .setApplicationId("1:something:android:something_else") // Required for Analytics.
       .setApiKey("your apikey") // Required for Auth.
       .setDatabaseUrl("https://your-database.firebaseio.com/") // Required for RTDB.
       .build();
FirebaseApp.initializeApp(this /* Context */, options, "secondary");

Source: The official Firebase blog

0

Comma seperated senderID solution is still working and able to register same token for 2 different sender. I sent push notif to that single magical token with using 2 different api key and able to receive push notifs for both api key. Hope it works at least till the end of 2020. Because I'm trying to make a seamless transition between an old GCM and FCM projects which targets more than 1 million user. (hear me google and thank you google for not deprecating this great solution)

String magicalToken = FirebaseInstanceId.getInstance().getToken("senderId, anotherSenderId", "FCM");

asozcan
  • 1,370
  • 1
  • 17
  • 24
-2

You can get the single token for multiple sender by passing them as comma separated string and then these sender will be able to send the push notification using the common token, try calling

FirebaseInstanceId.getInstance() .getToken("senderId1,senderId2", FirebaseMessaging.INSTANCE_ID_SCOPE);

make sure you call this from a background thread.

44kksharma
  • 2,740
  • 27
  • 32