gpt4 book ai didi

java - 在运行时选择 Firebase 数据库

转载 作者:行者123 更新时间:2023-11-29 23:16:17 25 4
gpt4 key购买 nike

我想让用户选择要使用的 Firebase 实时数据库。本质上,我在一个 apk 中使用多个 Firebase 数据库。由于这种情况发生在运行时,我将无法使用 google-services.json 文件连接到 Firebase。我引用了这篇博客,但仍然无法连接到数据库。


Blog post

博文 - https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html?m=1


有没有可以在运行时连接到 Firebase 数据库的解决方案。

最佳答案

如果不能依赖google services插件从google-services.json中读取数据,也可以在初始化FirebaseApp时在代码中指定相关数据 实例:

FirebaseOptions options = new FirebaseOptions.Builder()
.setApiKey("AI...j0")
.setApplicationId("1:5...e0")
.setDatabaseUrl("https://myapp.firebaseio.com")
.build();
FirebaseApp firebaseApp = FirebaseApp.initializeApp(getApplicationContext(), options, "second app");
FirebaseDatabase database = FirebaseDatabase.getInstance(firebaseApp);
database.getReference().setValue(ServerValue.TIMESTAMP);

您必须从 google-services.json 或 Firebase 控制台获取相关值。

另见我的回答:How to connect to more than one firebase database from an android App

关于java - 在运行时选择 Firebase 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55310719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com