0

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?

Atharva
  • 7
  • 3

3 Answers3

2

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

enter image description here

enter image description here

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.

Oleg
  • 591
  • 5
  • 14
0

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.

Thanasis M
  • 1,144
  • 7
  • 22
-1

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
rahat
  • 1,840
  • 11
  • 24