In our application development life cycle, we all have to deal with things like web service management, parsing of data and using parsed data in our mobile application. We also set up about push notifications about data changes in order to keep every user updated about the system. And we also have taken tension of third-party integration like Facebook or Google in order to authenticate data. To keep all these things in one place, Firebase technology has arrived!

How will Firebase ease the development approach?

Firebase is a BAAS (Backend As A Service) technology which helps users to keep their data on the server. Users can store and access their data for their mobile application and have instant updates if any changes occur in the database.

Some important features:

  • Data is stored in key-value pairs. Means, it will be structureless.
  • Users do not have to worry about data change notifications. If any data change occurs, it will be reflected directly to the client side.
  • Firebase provides authentication system via Google or Facebook so developers can control data access.
  • Mainly, we can set up Firebase in few minutes!

Steps to setup Firebase are as follows:

1.    Create an account

Go to the following link and create your account https://www.firebase.com/login/

You can sign up for your Google or Github account also.

2.    Include Firebase in your project

Add following dependency within your project.
dependencies {
compile 'com.firebase:firebase-client-android:2.5.2+'
}

You will have a problem of duplicate packages. To remove those errors, include following lines in your dependencies.

android {
    ...
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE-FIREBASE.txt'
        exclude 'META-INF/NOTICE' }}


Now your build gradle file will look something like this:

3.    Add permissions

Firebase requires android internet access permission. Add following permission to your manifest file.

<uses-permission android:name="android.permission.INTERNET" />

4.    Setup Firebase context

The Firebase library must be initialized once with an Android context. This must happen before any Firebase app reference is created or used otherwise your application will crash.To do this you can set the android context in your app’s application class.

5.    Do simple read and write operations

That’s it! You have set up Firebase on your application and you can now use it for database management!  You have to create an instance of firebase to access Firebase data.


Example:

 Firebase mFirebaseRef = new Firebase(FIREBASE_URL).child("chat");

Here FIREBASE_URL is the string of your database and child is a particular node name where you have to save data.

Write data

You can write data by accessing instance you created.

myFirebaseRef.child("chat").setValue("Hii how are you?");

Read data

To read data from the database, We have to add listeners in the following way.

myFirebaseRef.child("chat").addValueEventListener(new ValueEventListener() {

  @Override
  public void onDataChange(DataSnapshot snapshot{
    System.out.println(snapshot.getValue());  //prints "Hii How are you?"
  }

  @Override public void onCancelled(FirebaseError error) { }

});

Now whenever new data is inserted, updated or removed on chat node of Firebase, it will be listened by the listener we have created. We can access that value according to our needs.

So this was the very basic setup of Firebase, you can access security features, third-party authentication and much more!

You can visit Firebase website for more details.

Further reading:

For more information and latest updates, you can visit the official firebase website. You will find the official tutorials for android development here so you do not need to refer other websites for the tutorial.

https://www.firebase.com/