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

Viewed 599

How can I set the sender id when requesting a registration token for Firebase messaging in Android?

I need this, because I have to have different FB projects for the apps and the server that is sending the notifications. It's described here, but I think the doc is missing something. Searching StackOverflow results in answers that are not up-to-date, also. Can't find the way to do it with the latest version of the API.

3 Answers

I suggest having a read on controlling your Firebase registration/instance it also explains not only how to manually initialise your own firebase with custom options/name but also how firebase initialises itself automatically using the Google plugin and json file which will help you better understand what solution may be best for you.

Essentially you need to manually initialise your Firebase service in your Android apps Application class and provide a FirebaseOptions.Builder class to the Firebase.initializeApp, which allows you to call setGcmSenderId among other things like api key etc.

This official link shows how to configure mutliple projects in your application which may be an option too.

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.

the sender id and project details are set using configuration file "google-services.json",it reads this file from app folder of your android project. enter image description here

to get google-services.json for your firebase project in firebase console, open project settings and select your app and download the file and place it inside app folder of your android project. enter image description here

Related