Android Push Notification Using Firebase Cloud Messaging (FCM)

In this tutorial you will learn to implement android push notification using firebase cloud messaging (FCM).

Firebase is a real time cross platform database that provides various functionalities like authentication, storage, notification, etc. Firebase is becoming popular among developers due to its simplicity and easy implementation. In earlier days Google Cloud Messaging (GCM) was used to implement push notification. But Firebase Cloud Messaging (FCM) is better and easy to implement than GCM.

Android Push Notification Using Firebase Cloud Messaging (FCM)

Android Push Notification Using Firebase Cloud Messaging (FCM)

Prerequisites

To use FCM there are few minimum requirements.

  • Device with Android 2.3 or newer and Google Play services 9.6.0 or newer
  • The Google Repository from the Android SDK Manager
  • Android Studio 1.5 or higher version.

 

Android Project

Create a new android studio project with package name com.pushnotificationexample and copy the package name.

 

Firebase Project

Go to Firebase console https://firebase.google.com/console/ to create a new project.

Click on Create New Project button. Enter some project name and your country. Click on Create Project button to create the firebase project.

Android Push Notification Using Firebase Cloud Messaging (FCM)

Choose Add Firebase to your Android app option.

Android Push Notification Using Firebase Cloud Messaging (FCM)

Enter the package name that you copied earlier and click Add App button. You will get a google-services.json file.

Android Push Notification Using Firebase Cloud Messaging (FCM)

 

Add Firebase to Android Project

Select Project view in Android Studio and paste the google-services.json file under app folder.

Android Push Notification Using Firebase Cloud Messaging (FCM)

Open project level build.gradle file and add following line under dependencies.

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

 

Open app level build.gradle file and add following line under dependencies.

compile 'com.google.firebase:firebase-core:9.6.0'

 

Also add following line at the end of app level build.gradle file.

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

 

Firebase Cloud Messaging (FCM) Implementation

Create a new class MyFirebaseMessagingService that extends FirebaseMessagingService. Add following code inside it.

package com.firebasepushnotification;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;

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

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //calling method to generate push notification
        sendNotification(remoteMessage.getNotification().getBody());
    }

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Push Notification Example")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());
    }
}

 

Now we have to add above service in AndroidManifest.xml. So add following code anywhere between <application> <application/> tag.

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

 

Build and run your app.

 

Sending Push Notification

Go back to Firebase Console and select Notifications option from left menu. Click on New Message button.

Add a message in Message text box. Choose Target as User Segment. This will send the notification to all the users using this android app. You can also send notification to a particular user’s device.

Choose the app package name from drop down menu. Finally click on Send Message button to send the message.

Android Push Notification Using Firebase Cloud Messaging (FCM)

If you have done everything correctly then you will see a push notification in your device.Android Push Notification Using Firebase Cloud Messaging (FCM)

 

Comment below if you are facing any problem in above android push notification tutorial.

4 thoughts on “Android Push Notification Using Firebase Cloud Messaging (FCM)”

Leave a Comment

Your email address will not be published. Required fields are marked *