Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to more than one firebase database from an android App

I'm trying to have one project in Firebase that will be responsible for one common thing that all Apps.

That is, I want to create Apps, then have these Apps access a particular Firebase Database of a project.

Looking at the Firebase Android docs, I can't find a way to send data to another firebase database in another project using the following, but where reference is of another project.

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("example");
        ref.push().setValue(d).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                finish();
            }
        });
like image 876
Relm Avatar asked Jun 04 '16 20:06

Relm


People also ask

Can you connect more than one Firebase project to your Android app?

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.

How I connect Firebase database in Android application?

Open the Firebase Assistant: Tools > Firebase. In the Assistant pane, choose a Firebase product to add to your app. Expand its section, then click the tutorial link (for example, Analytics > Log an Analytics event). Click Connect to Firebase to connect your Android project with Firebase.

How multiple developers can work on the same Android app connected to a single Firebase console?

You can't have two projects of the same package name. Even if you delete it. It will take a least 4-5 days to get deleted fully from the developer's console. So the only solution is to generate a new SHA-1 key by custom signing the app by generating a signed apk from the android studio.


1 Answers

You'll need to initialize a second FirebaseApp object with explicit options in your code:

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

I got the configuration values from the second project's google-services.json. The API Key is under a property called api_key, the Application ID came from a property called mobilesdk_app_id and the database URL came from a property called firebase_url.

Also see the documentation on using multiple projects in your application.

like image 160
Frank van Puffelen Avatar answered Oct 01 '22 11:10

Frank van Puffelen