Android客户端FirebaseApp初始化错误 [英] Android client FirebaseApp initialization error

查看:239
本文介绍了Android客户端FirebaseApp初始化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实施了GCM,现在正尝试将GCM导入FCM.我不想使用google-services.json,因为我想在不同的环境(Dev,Stag,Prod)中使用不同的google项目.

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();

我在Firebase App初始化过程中一直遇到异常情况

I keep getting exception on Firebase App initialization

原因:java.lang.IllegalStateException:名称为[DEFAULT]的FirebaseApp不存在.可用的应用程序名称:MyTestFCM

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

如果我删除应用程序名称,那么我将不会获得令牌,然后当我重新运行应用程序时,我将获得令牌.我试图从Mainactivity和Application类执行以上代码.在两个地方,我得到相同的错误.

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.

所以我的问题是如何在不使用google-services.json的情况下初始化Firebase应用程序

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

谢谢, P

推荐答案

因此,您需要将google-services.json用于FCM,并处理多种构建变体情况,

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

  1. 从firebase控制台分别为不同的构建变体生成google-services.json.
  2. 下载google-services.json
  3. 在您的项目中,为变体创建文件夹,例如,如果要发布版本,请创建一个名称为"release"的文件夹,并将google-services.json放入其中.

在构建应用程序时,根据构建版本的不同,它会在相应文件夹中查找google-services.json并从中获取项目详细信息.

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.

可以在此线程上找到更多详细信息 Google服务问题54

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

感谢亚瑟·汤普森

这篇关于Android客户端FirebaseApp初始化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆