August 11, 2016

In this tutorial I’m going to show you how to implement Firebase in your application. Along with the full code, in the end of this post you will find “My YouTube Video” link also.

WHAT IS FIREBASE ?

Firebase is a unified mobile development platform from Google. With a single suite of tools, app developers & marketers have the resources to develop great apps, grow and re-engage users, and monetize their app. There are 15 features within Firebase, that work across Android, iOS and web. Where applicable, these features work seamlessly together for an intuitive, powerful developer experience.

WHAT ARE THE PREREQUISITES TO IMPLEMENT FIREBASE?

1. Android Studio 1.5 or Higher
2. Android Device 2.3 or Higher
3. Google Play Service 9.4.0 or Higher

STEPS TO IMPLEMENT FIREBASE :

  1. Create Android Project
  2. Open console.firebase.google.com and create a new Project
  3. Add new app to the project and download ‘google-services.json’
  4. Copy the downloaded file of ‘google-services.json’ and paste it in your project.
  5. Add Gradle Dependencies. Here we have to add dependencies in two places. One is the root level gradle and the other is your app gradle.
  6. a). Adding it in root-level gradle :

    Under dependencies add this :

    
    	 classpath 'com.google.gms:google-services:3.0.0'
    

    b).Addintg it in project-level gradle :

    Under dependencies add this :

    
    dependencies {
     compile 'com.google.firebase:firebase-core:9.4.0'
     compile 'com.google.firebase:firebase-messaging:9.4.0'
    }
    // Add this line in the bottom of the gradle file.
    apply plugin: 'com.google.gms.google-services'
    
  7. Create two Services:
    a). FirebaseInstanceIdServices, and
    b). FirebaseMessagingServices
  8. Open Manifest and add Internet Permission and register the above two mentioned services.
    Add these Permission :

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

    Register the Services :

    <service android:name=".MyFirebaseMessagingService">
                <intent-filter>
    
                    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    
                </intent-filter>
    
            </service>
            <service android:name=".MyFirebaseInstanceIdServices">
                <intent-filter>
                    <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    
                </intent-filter>
    
            </service>
    
  9. In MainActivity add the below line:
    FirebaseInstanceId.getInstnce().getId();

BUILD AND RUN YOUR PROJECT.

Up to here our coding work is done, now open your Firebase console.

In the console, on the left hand side you will see a “Notifications” tab. Click on it. After clicking few more fields you will have to enter. After filling everything Just click “Send Message”. You will be able to find a new notification on your mobile device.

Below I’m writing all the Code I used in the Project.

MainActivity.java


package com.firebasesample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.google.firebase.iid.FirebaseInstanceId;

/**
 * Created by Techno Blogger on 01/08/16.
 */

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FirebaseInstanceId.getInstance().getId();
    }
}

FirebaseMessagingServices.java


package com.firebasesample;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

/**
 * Created by Techno Blogger on 01/08/16.
 */
public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
      //write your foreground notification code here(optional)
    }
}

FirebaseInstanceId.getInstance.java


package com.firebasesample;


import com.google.firebase.iid.FirebaseInstanceIdService;

/**
 * Created by Techno Blogger on 01/08/16.
 */
public class MyFirebaseInstanceIdServices extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();

        //code to register gcm id on server if require
    }
}

Manifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.firebasesample">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <service android:name=".MyFirebaseMessagingService">
            <intent-filter>

                <action android:name="com.google.firebase.MESSAGING_EVENT"/>

            </intent-filter>

        </service>
        <service android:name=".MyFirebaseInstanceIdServices">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>

            </intent-filter>

        </service>

    </application>

</manifest>

Build and Run your App. For clear understanding, I’m providing the link of my YouTube Video too.
https://www.youtube.co