Firebase Realtime Database In Android

Welcome to another post of Firebase series. We have already discussed about Firebase Remote Config, Firebase Authentication and Firebase Crash Report. Today we are going to discuss about Firebase Realtime Database.

As an android developer we always have a feeling that if we need to sync data between more number of users we need :

  • Webservices/API
  • MySQL database
  • Some domain
  • Server to host everything.

With Firebase Realtime Database we can avoid all above things. It is a database hosted on cloud. It is a NoSQL database. You will be allowed to store and sync data in JSON format. Each data will be stored as a JSON.

Whenever there is any change in database, other connected user will be notified with data change within a milliseconds.

Firebase Realtime Database can also work in offline, it will persist user data on disk. Once connectivity is reestablished, the client device receives any changes it missed, synchronizing it with the current server state.

Setup Firebase Realtime Database

Step 1 : Create a new project on Firebase Console. Enter you project name and select country, than click CREATE PROJECT

Firebase Realtime Database

Step 2 : After creation of project click on Add Firebase to your Android app. Enter package name, App nickname(optional) and SHA-1 key(optional) and click on ADD APP.

Firebase Realtime Database

Step 3 : You will receive google-services.json file, put it inside app/ folder of your project.

Firebase Realtime Database

Step 4 : Add dependency to your root level build.gradle file.

Step 5 : Add dependency to your app/module level build.gradle file.

Step 6 : Add plugin at the bottom of your app-level build.gradle file.

After adding google.json file and all setup you might get this error

Error:Execution failed for task ‘:app:processDebugGoogleServices’.
> Missing api_key/current_key object

It is a bug in google-services:3.0.0, but there is no need to worry about, once you add any service of the Firebase, replace the google.json file with newly created file. You can get new google.json file from Project Settings in Firebase console.

That’s it with the setup, not lets dig into coding part. But before going into it, take a look at Firebase rules also.

Default : It allows authenticated users to read and write data. They are useful if you want data open to all users of your app.

Public : It allows everyone to access your application database i.e. anyone can read and write data.

User : It gives each authenticated user a personal node at /users/$user_id where $user_id is the ID of the user obtained through Authentication.

Private : It will disable read and write operation for all users. Admin can only access data from Firebase Console.

In order to write data to Firebase Database, you need an instance.

Create a reference of your parent node and write data into it.

It will create a node named “version” and writes value into it.

Firebase Realtime Database

Read data added to database

Here we have added string data so we can write dataSnapshot.getValue(String.class). If it is other datatype, you can mention it there.

Write an object to the database :

It will create a node named “user” and writes data into it.

Firebase Realtime Database

Structured Data 

As we know this is not the right way to write the data. We can have multiple users so data should be structured. Lets see how to make it.

push() will create a unique id and User data will be stored inside that.

Firebase Realtime Database

Listener for particular node

Whenever there is a change in specified node, onDataChange will be called on each active users device.

Still there are many things to learn about in Firebase Realtime Database. If it is possible i will come once again with some realtime applications. Until then enjoy coding 🙂

Firebase Crash Reporting In Android

  • Chobela Kakumbi

    Where can I find the source code?