Android Push Notification Using Firebase

Firebase is a very nice services of Google specially for mobile   platform . Apps developer can do many things simply . Firebase has several features including real-time database , hosting, messaging , notifications ,analytics , app-linking ,admob and so on .

I will discuss here push notification using firebase including big image notification.

  1. Firebase Account and Project Creation : 

Go to firebase console and login with gmail account and  press button ‘Create New Project’

1

Give your project Name and select country and press create project .

2. Android Project Creation :

Create an android empty-Project  . Open the gradle file (app module ).

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:24.0.0'

    compile 'com.google.firebase:firebase-messaging:9.4.0'

}

apply plugin: 'com.google.gms.google-services'

Open gradle file (project ) and add gradle

classpath 'com.google.gms:google-services:3.0.0'
buildscript {
    repositories {
        jcenter()
        mavenLocal()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
        classpath 'com.google.gms:google-services:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        mavenLocal()
    }
}

3. Notification Settings on Firebase Project Console :

Click ‘Add Firebase on your android app’ and Paste your application package name . Application Package name can be found on manifest file or gradle file .

23Click ‘Add App’ then your will get an google-services.json file . Copy it and paste it on your project on ‘app’ directory .

And Again go to your firebase project console and click next and finish . ok ?

4. Service and Notification add On android projects:

Create a Service Class Named

MyFirebaseMessagingService
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

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

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());
        }

    }

    public void sendNotification(String messageBody) {

        android.support.v7.app.NotificationCompat.Builder nb= new android.support.v7.app.NotificationCompat.Builder(this);
        nb.setSmallIcon(R.drawable.icon);
        nb.setContentTitle("New Post : Today's Hadish");
        nb.setContentText(messageBody);
        nb.setTicker("Take a look");
        nb.setAutoCancel(true);



        //get the bitmap to show in notification bar
        Bitmap bitmap_image = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
        android.support.v7.app.NotificationCompat.BigPictureStyle s = new android.support.v7.app.NotificationCompat.BigPictureStyle().bigPicture(bitmap_image);
        s.setSummaryText(messageBody);
        nb.setStyle(s);



        Intent resultIntent = new Intent(this, MainActivity.class);
        TaskStackBuilder TSB = TaskStackBuilder.create(this);
        TSB.addParentStack(MainActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        TSB.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                TSB.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        nb.setContentIntent(resultPendingIntent);
        nb.setAutoCancel(true);
        NotificationManager mNotificationManager =
                (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(11221, nb.build());



    }

}

Create another Class and Named :

MyFirebaseInstanceIDService
import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;


public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        sendRegistrationToServer(refreshedToken);
    }

    private void sendRegistrationToServer(String token) {

    }
}

Go to your manifest and add the service registration :

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

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

and Add Internet Permission :

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

5. Run App:

Run your app on real devices .

6. Send Notifications :

Go to firebase console and go to Notification tab and press ‘send your first notification ‘

6

If every thing is ok then you should show  notification .

Download Source Code