How to Put Multiple Project_Number/Sender Id in Google-Services.JSON

How to put multiple project_number/sender id in google-services.json

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.

google-services.json for different productFlavors

Google included support for flavors in version 2.0 of the play services plugin. Since this version of the gradle plugin com.google.gms:google-services:2.0.0-alpha3

you can do this

Step 1: add to gradle

// To auto-generate google map api key of google-services.json
implementation 'com.google.android.gms:play-services-maps:17.0.0'

Step 2: add to AndroidManifest.xml in the application tag

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key" />

Step 3: download each flavor JSON file from firebase and add it

app/src/
flavor1/google-services.json
flavor2/google-services.json

Version 3.0.0 of the plugin searches for the JSON file in these locations (considering you have a flavor flavor1 and a build type debug):

/app/src/debug/google-services.json
/app/src/debug/flavor1/google-services.json
/app/google-services.json

This worked for me even using flavorDimensions. I have free & paid in one dimension and Mock & Prod in the other dimension. I also have 3 buildTypes: debug, release, and staging. This is how it looks in my project for the FreeProd flavor:

Sample Image

How many google-services.json files will depend on your project's characteristics, but you will need at least one JSON file for every Google project.

If you want more details about what this plugin does with these JSON files, here it is:
https://github.com/googlesamples/google-services/issues/54#issuecomment-165824720

Link to the official docs:
https://developers.google.com/android/guides/google-services-plugin

Blog post with updated info: https://firebase.googleblog.com/2016/08/organizing-your-firebase-enabled-android-app-builds.html

And go here to check the latest version of this plugin: https://mvnrepository.com/artifact/com.google.gms/google-services?repo=google

Firebase messaging - how to set Sender Id when requesting a token in Android?

I wanted to keep using the google-services.json, so I didn't want to do the manual init. Then when I read how the Google Services Gradle Plugin works, I was going to make the manual init by using the values from the file - more info here: https://developers.google.com/android/guides/google-services-plugin#processing_the_json_file

In the end, I ended up not changing anything in the code and just changing project_info/project_number in the google-services.json. It's used for gcm_defaultSenderId. Everything looks good, now. Hope it's not used for anything, else. We are using FCM, only, so I think it will be ok. I don't like this, but it will do.

Given a FCM sender id, can any one send msg to our server?

From FCM support:

After discussing with our engineers, it seems that FCM doesn't have an authorization mechanism for upstream messages. We will be working on a fix for this, although we can't share any timelines for now as to when the fix with be available. You may keep an eye out on our release notes for further updates.

It is a bug of firebase



Related Topics



Leave a reply



Submit