LizG
LizG

Reputation: 2530

How to have two different languages in one Firebase project?

I have developed my Android app using Firebase and I have developed an iOS app using Firebase. I created two different projects in Firebase, thinking I had to. Now, in order to get the two to talk to each other, I was told to put both in the same project.

I was referred to go to configuring multiple apps but after reading it, I am a bit confused. Any clarification would be greatly appreciated.

I want to connect my iOS project to the Android project I have.

I understand that I need to go the Overview and add another app and select IOS.

I am assuming once I do this, I get a new GoogleService-Info.plist file? And I am also assuming I replace the new plist file with the old, correct?

What I don't understand is the part below:

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

I just don't know if I have to put the code in for both the Android and IOS project or just the IOS (where this is the one I will be adding to the Android project in Firebase).

Upvotes: 0

Views: 491

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599166

The documentation page you're referring to describes how to connect a single app to multiple Firebase projects. Your scenario is the exact opposite: you have multiple apps that you want to connect to a single Firebase project.

To do this, you simply:

  1. Create an Android app in your Firebase project, download the google-services.json, and add it to your Android project.
  2. Create an iOS app in your Firebase project, download the google-services.plist, and add it to your iOS project.

So... ignore the documentation page you quoted, and instead just add an Android app, and an iOS app to the same project. They can then access the same back-end services.

Upvotes: 2

Related Questions