Getting Started With Firebase Android

Ravi Rupareliya
6 min readJan 28, 2017

In I/O 2016 google released new and upgraded tool called Firebase. As a mobile developer when you are building large scale applications you need back-end support as well as a special developer who can work with server level stuffs.

Firebase provides all those kind of functionality which are needed from server side, in addition to Realtime Database, Storage and Hosting, it also provides Authentications, Analytics, Notifications, Crash Reporting and much more.

Source : Firebase

In this series of Firebase we will take a detailed look at almost all features provided by Firebase.

Firebase Analytics :

Firebase Analytics help you to analyse how your user is using the application. It is totally free and provides up to 500 distinct events.

Firebase Analytics help you to understand your app users need, what they want, how they are doing with the app, what are the things they are engaging most in the app.

Realtime Database :

Firebase provides NoSQL realtime cloud database. Firebase Realtime Database stores data in JSON format and is synchronized with all the connected clients.

When you integrate Realtime Database Android, iOS or JavaScript SDK, one realtime database instance will be shared with all the users. Every client will receive an update when there is any change in data.

Authentication :

Using Firebase Authentication user will be authenticated via multiple ways, it can be email, Facebook, Twitter, Google or Github.

Firebase Authentication allows you to create new user and store user’s credentials to Firebase Database.

With the Firebase Authentication support now you do not need to integrate that heavy server configuration or APIs to be deal with login and signup process.

You can even send a confirmation email after registration and it also provide forgot password functionality in case of user forgets their credentials.

Crash Reporting :

Crash Reporting is very important and helpful tool for any developer. With Firebase Crash Reporting you will get whole crash report in firebase developer console. Report consist of OS details with error detail and other factors.

You can also generate your custom Crash Report with Firebase. It will help you to rectify the actual error in your project.

Cloud Messaging :

With Firebase Cloud Messaging(FCM) you will be able to engage with users in periodic time. Generate periodic notifications that reminds user about your application.

You can even use Cloud Messaging to let your user knows about promotional or seasonal offers available in your application. FCM allows you to transfer payload of up to 4KB to client application.

FCM also allows you to send acknowledgement or chat message back to the your server over FCM’s reliable and battery-efficient connection channel.

Remote Config :

Remote config is the best feature from developer point of view, it allows you to perform some dynamic operation without updating the build. End user will get the latest changes without updating application to new version.

You can use Remote Config to design your app based on user, version name, locality etc etc criteria. Remote config can be used to show some promotional banners while your app runs or show some extra ordinary UI to some special users.

App Indexing :

App Indexing allows you to re-engage with your users. If google finds any searching term related to your app and if app is installed, user will directly be redirected to the app by clicking on the link.

App Indexing helps your user to search content in most easy way via google.

Invites :

Firebase Invites is the most efficient way to increase users in your app. If your app is having good content and user engagement, your user is going to share it with others also and that is the best way to increase app users.

Word of mouth is one of the most effective ways of getting users to install your app.

AdMob :

After doing everything with your application, we need to monetize our app to earn money. After all this is the only thing for which we were doing everything.

AdMob is google’s advertise platform which allows to earn money through your app.

In the next part of this series we will take a look at some programming guideline for Firebase features.

Setup Firebase Remote Config

Step 1 : Create a project on Firebase Console .

Step 2 : After successful creation of your first project Click on Add Firebase to your Android app.

Step 3 : Add all the details of your project like Package name, App nickname, SHA — 1 key etc and download google.json file. Put that file in app/ folder

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

dependencies {    classpath ‘com.google.gms:google-services:3.0.0’}

Step 5 : Add dependency to your module level build.gradle file and add apply plugin at bottom of file

dependencies {    ...    compile 'com.google.firebase:firebase-config:10.0.1'}apply plugin: 'com.google.gms.google-services'

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.

Step 6 : From Firebase Console setup your Remote Config by clicking on Remote Config from left menu.

Step 7 : Click on Add Your First Parameter will prompt a new dialog asking for parameter key and default value. After adding the parameter don’t forget to click publish changes.

Step 8 : Setup default value of config parameter in your android project in form of Map or XML resource File. Store your xml file inside res/xml. Here we have created default_config.xml file

<?xml version="1.0" encoding="utf-8"?><!-- START xml_defaults -->
<defaultsMap>
<entry>
<key>background_color</key>
<value>#000000</value>
</entry>
</defaultsMap>

Note : Make sure that parameter name/key name must be the same as we have created in Firebase Console.

Step 9 : Get an instance of FirebaseRemoteConfig and set default values using setDefaults()

FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
firebaseRemoteConfig.setDefaults(R.xml.default_config);

Step 10 : Fetch the value of background_color key by using firebaseRemoteConfig object

String bg_color = firebaseRemoteConfig.getString("background_color");
relativeMain.setBackgroundColor(Color.parseColor(bg_color));

Now you can build and run the application and you will see black background as it is default value we have provided in default_config.xml file

Now we will fetch the value from our server/console which we had already set to white(#FFFFFF). To fetch the value we need to use fetch() which will get latest value of all the parameters available.

firebaseRemoteConfig.fetch().addOnCompleteListener(this, new OnCompleteListener&lt;Void&gt;() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// Once the config is successfully fetched it must be activated before newly fetched
// values are returned.
firebaseRemoteConfig.activateFetched();
String bg_color = firebaseRemoteConfig.getString("background_color");
relativeMain.setBackgroundColor(Color.parseColor(bg_color));
} else {
Log.d("TAG", "Fetch failed");
}
}
});

As you can see in above code after fetching values successfully we have used firebaseRemoteConfig.activateFetched(); which will activate all the fetched values and new values will be reflected to our existing configuration.

Not only just background color, you can also use this feature to show some promotional banner, just fetch Boolean value from server and based on that show some dialog which describes offer you are providing.

This article is originally posted on my blog androidgig.com

Thanks for reading this article. Be sure to click ❤ below to recommend this article if you found it helpful. It means a lot to me.

--

--

Ravi Rupareliya

React Native | Android. Team Manager @simform Find more about me on http://ravirupareliya.com.