top of page
admin

How to Connect to Multiple Firebase Databases with an Android App

Updated: May 19, 2020

This is the first article in our series about Firebase. Here are more posts in the series:


Firebase is a really useful database tool suite from Google. It has many nice features for the developer who isn’t looking to architect their own database solution or deal with a lot of networking code. For example, Firebase automatically handles local caching, and will deal with connectivity interruptions without the developer having to set anything up to handle these conditions. Firebase is primarily oriented towards an app connecting to a single firebase database instance, but there are times when one might want to have an app connect to multiple databases without having to build separate flavors. For our use case, we wanted to have separate databases to isolate data from different clients, but we didn’t want to have to manage a bunch of different app flavors.


Adding one firebase database connection to your app is easy. Firebase does a good job of walking you through it. In the interest of completeness, I’ll list the steps here:


  1. From the firebase console, select your database instance, then click ‘add another app’.

  2. Select ’Android’, then input your package name into the appropriate field.

  3. Download the google-services.json file from the console, and put in your /app directory for your app.

  4. Add the listed dependencies to your build.gradle file. You will also want to set up auth, which will probably be using some email/google accounts.


The app will now handle all of the Firebase connection details for you. If you want to add additional Firebase database connections, however, you will have to do a bit more coding.


First, be sure that your app is registered in all of the database instances that you want to connect to. You will only be able to use one google-services.json file, but you will need all of the databases to recognize your app. You will also want to set up your email addresses appropriately in all of the database instances for authorization functionality to work.


In your code, you will need to fill out the following snippet for each extra instance:

       FirebaseOptions options = new FirebaseOptions.Builder()
                .setApplicationId("YourApplicationId")
                .setApiKey("yourApiKey")
                .setDatabaseUrl("https://your-second-database.firebaseio.com/")
                .build();
        FirebaseApp.initializeApp(this.getContext(), options, "second_database_name");
        FirebaseApp secondApp = FirebaseApp.getInstance("second_database_name");

To connect to your different databases, you would do the following:

FirebaseDatabase firstDatabase = FirebaseDatabase.getInstance();  //default db from JSON
FirebaseDatabase secondDatabase = FirebaseDatabase.getInstance(secondApp);

You can do this as many times as you like, but you can only have one connection to any given database instance at once.


If you would like help programming your app, please contact us. We offer a variety of programming services, from developing mobile and web applications to payment terminal integrations.

7,917 views0 comments

Comentários


bottom of page