How to connect to more than one firebase database

2019-01-01 13:34发布

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

1条回答
其实,你不懂
2楼-- · 2019-01-01 14:26

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.

查看更多
登录 后发表回答