android - 如何在一个 Firebase 项目中使用两种不同的语言?

标签 android ios firebase

我使用 Firebase 开发了我的 Android 应用程序,并使用 Firebase 开发了一个 iOS 应用程序。我在 Firebase 中创建了两个不同的项目,认为我必须这样做。现在,为了让两者相互交谈,我被告知将两者放在同一个项目中。

我被转介到 configuring multiple apps但是看完之后,我有点糊涂了。如有任何澄清,我们将不胜感激。

我想将我的 iOS 项目连接到我拥有的 Android 项目。

我知道我需要转到概览并添加另一个应用并选择 IOS。

我假设一旦我这样做,我就会得到一个新的 GoogleService-Info.plist 文件?而且我还假设我用旧的替换了新的 plist 文件,对吗?

我不明白的是下面的部分:

Use multiple projects in your application

Sometimes you need to access different projects using the same APIs - for example, accessing multiple database instances. In most cases there is a central Firebase application object that manages the configuration for all the Firebase APIs. This object is initialized as part of your normal setup. However, when you want to access multiple projects from a single application, you’ll need a distinct Firebase application object to reference each one individually. It’s up to you to initialize these other instances.

In both cases, you need to first create a Firebase options object to hold the configuration data for the Firebase application. Full documentation for the options can be found in the API reference documentation for the following classes:

enter image description here

The use of these classes to support multiple projects in an application is shown in the following examples:

iOS:

// Configure with manual options.
let secondaryOptions = FirebaseOptions(googleAppID: "1:27992087142:ios:2a4732a34787067a", gcmSenderID: "27992087142")
secondaryOptions.bundleID = "com.google.firebase.devrel.FiroptionConfiguration"
secondaryOptions.apiKey = "AIzaSyBicqfAZPvMgC7NZkjayUEsrepxuXzZDsk"
secondaryOptions.clientID = "27992087142-ola6qe637ulk8780vl8mo5vogegkm23n.apps.googleusercontent.com"
secondaryOptions.databaseURL = "https://myproject.firebaseio.com"
secondaryOptions.storageBucket = "myproject.appspot.com"

Android:

// Manually configure Firebase Options
FirebaseOptions options = new FirebaseOptions.Builder()
        .setApplicationId("1:27992087142:android:ce3b6448250083d1") // Required for Analytics.
        .setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw") // Required for Auth.
        .setDatabaseUrl("https://myproject.firebaseio.com") // Required for RTDB.
        .build();

After you have initialized this options object, you can use it to configure an additional Firebase application instance. Note that in all the examples shown below we use the string secondary. This name is used to retrieve the application instance, and to distinguish it from other instances, including the default instance (named [DEFAULT]). You should pick a string appropriate to the intended use of the other Firebase project.

The following snippets demonstrate connecting to an alternative Realtime Database (the APIs for other Firebase features follow the same pattern).

iOS:

// Configure an alternative FIRApp.
FirebaseApp.configure(name: "secondary", options: secondaryOptions)

// Retrieve a previous created named app.
guard let secondary = FirebaseApp.app(name: "secondary")
  else { assert(false, "Could not retrieve secondary app") }


// Retrieve a Real Time Database client configured against a specific app.
let secondaryDb = Database.database(app: secondary)

Android:

// Initialize with secondary app.
FirebaseApp.initializeApp(this /* Context */, options, "secondary");

// Retrieve secondary app.
FirebaseApp secondary = FirebaseApp.getInstance("secondary");
// Get the database for the other app.
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(secondary);

我只是不知道我是否必须将代码放入 Android 和 IOS 项目,还是只放入 IOS(这是我将添加到 Firebase 中的 Android 项目的代码)。

最佳答案

您所指的文档页面描述了如何将单个应用连接到多个 Firebase 项目。您的情况恰恰相反:您有多个应用程序要连接到单个 Firebase 项目。

为此,您只需:

  1. 在您的 Firebase 项目中创建一个 Android 应用,下载 google-services.json,并将其添加到您的 Android 项目。
  2. 在您的 Firebase 项目中创建一个 iOS 应用,下载 google-services.plist,并将其添加到您的 iOS 项目。

所以...忽略您引用的文档页面,而只是将一个 Android 应用程序和一个 iOS 应用程序添加到同一个项目。然后他们可以访问相同的后端服务。

关于android - 如何在一个 Firebase 项目中使用两种不同的语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53219706/

相关文章:

android - 我怎样才能得到每个 child ? Firebase 获取值 [android]

firebase - Flutter Apple 登录 - 空电子邮件

android - 使用导航组件的某些 fragment 的自定义 "navigate up"行为

java - 在动态创建的元素上添加 onClick 监听器不起作用

javascript - requireNativeComponent : "RNSScreenStackHeaderConfig" was not found in the UIManager when running android app

ios - 为什么即使我使用 Alamofire 发出请求,我的应用程序仍然卡住?

ios - AVPlayer View Controller 自动旋转关闭?

ios - Google 自动完成组件限制 iOS

ios - 为什么我的 iOS 应用程序禁用了 Firebase Analytics?

android - 如何通过后台服务在android中的特定时间每天重复通知