Use multiple Firebase projects in 1 Android app

Tram Ho

Hello friends !

The recent epidemic was unpaid at home, rowing like a cat. Fortunately, having the other German guy assign me an interesting project. It’s an application sort of like eMarket, ie he requires 3 roles

  • Client: Customers, enter the product order
  • Seller: Seller, allowed to list products and process order. When the order is processed, an order is available on the system
  • Shipper: Go to the system to see if there is a reasonable application, then pick that application to ship to make money Listening to it is okay, but he noted that I must set up separate databases on individual Firebase projects, not centralized. I don’t go into depth about why he wants that, personally I think he wants to manage the information separately, doesn’t want to put all the eggs in one place, so be it.

Before, I used to do multiple apps in a Firebase project, share the same database, but never did it like this guy said, so I’m very excited to do this. And after a few dozen minutes of tinkering and testing, I have built up a base project that works according to him, with the right requirements for separate databases. So, in that joy, I immediately compiled a tutorial, so that if I ever see the above case again, I still know how to handle it, and risk sharing with you to research and develop ways. better.

Okay, first, we create new individual Firebase projects, create a new Firebase project, let me take a look at it, because it’s just a few clicks and names.

Then, suppose I create 3 Firebase projects as follows: 1. EDMTDevClient 2. EDMTDevSeller 3. EDMTDevServer

Next, in the Android project, I will create a new class, following the Singleton design pattern as follows:

The above class declares an instance of type FirebaseApp, that is, returns 1 FirebaseApp object that was initiated according to the FirebaseOptions information configured above. What configuration information includes:

  1. Firebase Project Id
  2. Firebase Project Application Id
  3. Firebase API key
  4. Firebase Database Url P / s: Here I use Firebase Database Realtime, if you are interested in this problem, but on Cloud FireStore, I have to add a version for Cloud FireStore inbox. I think it’s the same hehe Then come back to any post, where are the 4 above information, please in Firebase. I will show you right here:

Go to Firebase Console , select Project Setting – there is a cog in the left corner and you will see the following picture

Here you have (1) Firebase Project Id and (3) Firebase API key. (The part I hide in red is that information, but it’s too sensitive so I should cover it)

Go back to Firebase Console and choose Project Overview , select an Android app (if you don’t have one, create a new one) and copy the App ID in that app info.

So you have information number (2) Project Application Id

Finally, Firebase Database Url, go to Firebase Console , on the left menu select Realtime Database and you will see

That green-veiled part

And so you have created a FirebaseApp and init according to the above information, so in order to use it, remember to reconfigure the Rules for Firebase, how many References, then configure the Rules The principle is: Which Ref does share share? Between apps, let READ / WRITE be auth! = null. Ref which is read-only, WRITE set False.

Then, in the Android code, if you want to use Firebase, you call the following

a. With Firebase Auth:

FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(EDMTDevClientConfig.getInstance(this));

b. With Firebase Database:

c. With Firebase Storage:

With this guy is a little different, the config is as follows:

Where can I get the Storage Url? You go to Firebase Console , the left menu select Storage and get it here

See, covered place, is there a leash you see? Clicking on it will copy the url for you!

Okay! With config there is complete, you can use Firebase project of the Client for other applications connected, only the init according to information on Similarly, you can completely redo the steps above to create EDMTDevSellerConfig and EDMTDevShipperConfig For cross use together.

My article is over here, if you find it interesting, don’t forget to drop “lai” and “crash”. Thank you !

Share the news now

Source : Viblo