Android client FirebaseApp initialization error

2019-06-01 06:44发布

I had GCM implemented and I am trying to import GCM to FCM now. I dont want to use google-services.json because I want to use different google projects for different environments(Dev, Stag, Prod).

I tried to initialize the app via this code below

 FirebaseOptions.Builder options=new FirebaseOptions.Builder();
       options.setGcmSenderId("<<project id>>"); 
       options.setApplicationId("<<Project name>>"); 
       FirebaseApp firebaseApp = FirebaseApp.initializeApp(getApplicationContext(),options.build(),"MyTestFCM");
       String refreshedToken= FirebaseInstanceId.getInstance(firebaseApp).getToken();

I keep getting exception on Firebase App initialization

Caused by: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. Available app names: MyTestFCM

If I remove the app name then i dont get a token back, then when I re-run the application I get a token. I have tried to execute the above piece of code from Mainactivity and Application class. In both places I get the same error.

So my question is how do I initialize Firebase application without using google-services.json

Thanks, P

4条回答
The star\"
2楼-- · 2019-06-01 06:51

have you checked to make sure to set your FIREBASE_URL to the right string? Also did you make sure to have the appropriate app setup config?

Last time this happened, I needed to set up my Android App Configs. https://support.google.com/firebase/answer/7015592

Afterwards, you can follow this setup. https://firebase.google.com/docs/cloud-messaging/android/client

查看更多
仙女界的扛把子
3楼-- · 2019-06-01 06:52

Firebase SDK requires to have a "default" project configured via google-services.json

Once you have that you can register FCM sdk with other sender-id using the FirebaseInstanceId.getInstance(context).getToken(...)

But please note that the most recent FCM features (auto-retry of the registration, topic subscriptions) only work with the default project.

查看更多
Evening l夕情丶
4楼-- · 2019-06-01 07:05

Please do double check, you added

apply plugin: 'com.google.gms.google-services' at the bottom of app's gradle file and then clean and rebuild the project

查看更多
霸刀☆藐视天下
5楼-- · 2019-06-01 07:07

So turns out that you need to use google-services.json for FCM, and to handle multiple build variants situation,

  1. Generate respective google-services.json from the firebase console for the different build variants.
  2. Download the google-services.json
  3. In your project create folders for the variants, for example if you want release build, create a folder with name "release" and place the google-services.json in it.

When the app builds, depending upon the build variant it looks for google-services.json in the respective folder and picks up the project details from it.

More details can be found on this thread google services issues 54

Thanks Arthur Thompson

查看更多
登录 后发表回答