Setting up Firebase for Android App

Firebase Messaging Cloud or Firebase is free cross-platform service for messaging and notifications that can be integrated into apps. It is a cloud NoSQL database that stores data in JSON, synchronizes in milliseconds, and works offline. Firebase provides intuitive and easy-to-use API and remains capable regardless of network latency or Internet connection.

Here I will demonstrate a way to set up the Firebase service for a generic Android application.

Preparatory steps, registration, and authorization

First I create a new empty markup project, called com.shakuro.firebase by clicking Create a project on the console – main Firebase administration tool.

firebase_new-project

To add Firebase to an Android app, do the following:

firebase_new_android

After that don’t forget to open your project in Android Studio and copy the downloaded google-services.json file into the Android apps module root directory.

Now in the build.gradle project’s level, let’s add the dependency of the relevant library.

See the Pen firebase 1 by moses kim (@moseskimjj) on CodePen.


Also, the following line needs to be added in the end of build.gradle under the app catalog:

See the Pen firebase 2 by moses kim (@moseskimjj) on CodePen.

This line connects a certain plugin to the Google Service project. This plugin processes the configured JSON and taps into the basic libraries of com.google.gms:google-services.

Voila! Firebase is bound to the app which can now be assembled.

Realization

To make it simple, let’s log in to the console, open the created database, and add JSON right in this pretty interface.

Now add our activity:

See the Pen rWoeKQ by moses kim (@moseskimjj) on CodePen.

At this point everything is almost done, however, there is no access to the database. For the testing purposes and simplicity, let’s make the database fully accessible by changing the access rules from “null” to “true”.

Now let’s launch the application.

firebase chat

And last thing – configure the button to process data into database.

See the Pen VmqaGL by moses kim (@moseskimjj) on CodePen.

It’s all good, but could be better if the authorization was available. Not a problem, let’s add authorization into our mini-project.

See the Pen firebase 5 by moses kim (@moseskimjj) on CodePen.

Adding:

See the Pen firebase 6 by moses kim (@moseskimjj) on CodePen.

And consequently,

See the Pen firebase 7 by moses kim (@moseskimjj) on CodePen.

The method itself and the sign in exception handler:

See the Pen firebase 8 by moses kim (@moseskimjj) on CodePen.

Outtakes

  • For messages storage we used the Realtime database and services for cloud NoSQL database administration. They provide storage and synchronization for all the connected devices. The new Firebase build the data is saved on the hard drive to remain responsive during disconnect and certainly, the data is synchronized after the connection is reestablished.
  • For authorization we used the Google.auth service, identification and user management tool. The identification is supported through the email-password pair, social networks (Facebook, Twitter, GitHub, Google+), and can be integrated with other existing authorization systems.
  • For remote configuration we used the Remote Config service that allows us to change and update parts of the apps without having to update the entire packages and go through the long checks in application stores. For safety reasons and performance you will need to figure out what you want to be updated remotely and set the required key-value pair parameters beforehand. After that you can change the application layout and functional by changing these parameters at the backend. For example, you can style the app’s layout update for a holiday and then switch back with no additional reviews. Like many other Firebase elements, notifications are integrated into Firebase Analytics. Thus, you can filter groups of users (formed by hundreds of parameters) and send updates specifically to them.

To wrap it up, I believe that from just a data storage service, Firebase turned into a universal platform for mobile developers with a friendly interface and super simple app integration mechanism. As you just saw, you can add Firebase to your app in a couple of clicks.

I think it would be a mistake to neglect such a useful and interesting tool. Try it out! It’s awesome.