简体   繁体   中英

Multiple Firebase projects in a single android application

I'm trying to build an android application using Firebase that demands two separate databases (teachers and students) in a single application. I searched for the solution all over the internet but all I got is this solution- Multiple Firebase projects in one app , which seems to be a great solution but I didn't understand.

So, how do I merge two firebase projects in a single application?

You don't need to implement the communities solution that is described in that link anymore.

According to this :

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.

you need to first create a Firebase options object to hold the configuration data for the Firebase application, like this:

// Manually configure Firebase Options. The following fields are REQUIRED:
//   - Project ID
//   - App ID
//   - API Key
FirebaseOptions options = new FirebaseOptions.Builder()
    .setProjectId("my-firebase-project")
    .setApplicationId("1:27992087142:android:ce3b6448250083d1")
    .setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
    // setDatabaseURL(...)
    // setStorageBucket(...)
    .build();

After you have initialized this options object, you can use it to configure an additional Firebase application instance.

For example, if you have another app instance named "otherApp", you can then do this:

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

// Retrieve secondary FirebaseApp
FirebaseApp secondary = FirebaseApp.getInstance("otherApp");

By this way you connect to an alternative Realtime Database.

Yes, you can follow that guide https://firebase.google.com/docs/projects/multiprojects#java .

Basically, you need to initiate the firebase app providing configuration manually

        // [START firebase_options]
    // Manually configure Firebase Options. The following fields are REQUIRED:
    //   - Project ID
    //   - App ID
    //   - API Key
    FirebaseOptions options1 = new FirebaseOptions.Builder()
            .setProjectId("my-firebase-project")
            .setApplicationId("1:27992087142:android:ce3b6448250083d1")
            .setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
            // setDatabaseURL(...)
            // setStorageBucket(...)
            .build();
    FirebaseOptions options2 = new FirebaseOptions.Builder()
            .setProjectId("my-firebase-project")
            .setApplicationId("1:27992087142:android:ce3b6448250083d1")
            .setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
            // setDatabaseURL(...)
            // setStorageBucket(...)
            .build();
    // [END firebase_options]


    // [START firebase_secondary]
    // Initialize with secondary app
    FirebaseApp.initializeApp(this /* Context */, options1, "first");
    FirebaseApp.initializeApp(this /* Context */, options2, "secondary");


    FirebaseApp first = FirebaseApp.getInstance("first");
    FirebaseApp secondary = FirebaseApp.getInstance("secondary");

You can get all required data from google-service.json

在此处输入图像描述

在此处输入图像描述

Later you can get a database from that FirebaseApp , for example

FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(secondary);

Then you just work with the project like before.

The example you mentioned can be used in the following way. You can create two main tables in firebase db and then you can do operations by

val reference = FirebaseDatabase.getInstance().getReference()
val teacher = reference.child("teacher")//refers to the teacher table
val student = reference.child("student")//refers to the student table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM