Skip to main content

Android Push Notification Using Firebase Cloud Messaging

Firebase Notifications is a free service that enables targeted user notifications for mobile app developers.


Built on Firebase Cloud Messaging and the FCM SDK, Firebase Notifications (Notifications) provides an option for developers and organizations seeking a flexible notification platform that requires minimal coding effort to get started, and a graphical console for sending messages. Using the Notifications console GUI, you can reengage and retain your userbase, foster app growth, and support marketing campaigns.

Its simple as it do not overload with server side scripts like as in GCM and its easy to integrate option

Sending notifications


  • A device running Android 2.3 (Gingerbread) or newer, and Google Play services 9.4.0 or newer
  • The Google Play services SDK from the Android SDK Manager
  • Android Studio 1.5 or higher
  • An Android Studio project and its package name


Complete Video Guide

Here is a video guide to integrate Firebase push Notification in few minutes


Step 1 : Add Firebase to your app

To add Firebase to your app you’ll need a Firebase project and a Firebase configuration file for your app.

  1. Create a Firebase project in the Firebase console, if you don’t already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.
  2. Click Add Firebase to your Android app and follow the setup steps. If you’re importing an existing Google project, this may happen automatically and you can just download the config file.
  3. When prompted, enter your app’s package name. It’s important to enter the package name your app is using; this can only be set when you add an app to your Firebase project.
  4. At the end, you’ll download a google-services.json file. You can download this file again at any time.
  5. If you haven’t done so already, copy this into your project’s module folder, typically app/.

Step 2 : Add the SDK to Your Project

If you would like to integrate the Firebase libraries into one of your own projects, you need to perform a few basic tasks to prepare your Android Studio project. You may have already done this as part of adding Firebase to your app.

First, add rules to your root-level build.gradle file, to include the google-services plugin:

Here it shows How it Works

buildscript {
    // ...
    dependencies {
        // ...
        classpath ''

Then, in your module Gradle file (usually the app/build.gradle), add the apply plugin line at the bottom of the file to enable the Gradle plugin:

apply plugin: ''

android {
  // ...

dependencies {
  // ...
  compile ''
  compile ''

apply plugin: ''
import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

     * Called when message is received.
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
    // [START receive_message]
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // [START_EXCLUDE]
        // There are two types of messages data messages and notification messages. Data messages are handled
        // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
        // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
        // is in the foreground. When the app is in the background an automatically generated notification is displayed.
        // When the user taps on the notification they are returned to the app. Messages containing both notification
        // and data payloads are treated as notification messages. The Firebase console always sends notification
        // messages. For more see:
        // [END_EXCLUDE]

        // TODO(developer): Handle FCM messages here.
        // Not getting messages here? See why this may be:
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    // [END receive_message]

     * Create and show a simple notification containing the received FCM message.
     * @param messageBody FCM message body received.
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);//**The activity that you want to open when the notification is clicked
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("FCM Message")

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

        notificationManager.notify(0 /* ID of notification */,;

Step 4 : Send a message from the Notifications console

Add MyFirebaseMessagingServiceServices to Application Tag of android manifest

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


Step 5 : Send a message from the Notifications console

  1. Install and run the app on the target devices.
  2. Open the Notifications tab of the Firebase console and select New Message.
  3. Enter message text.
  4. Select the message target. The dialog displays further options to refine the target based on whether you choose App/App Version, Device Language, or Users in Audience.

After you click Send Message, targeted client devices that have the app in the background receive the notification in the system notifications tray. When a user taps on the notification, the app launcher opens your app.


Download The Source code