Google Firebase Cloud Messaging (FCM) Push Notification Implementation In Android

Firebase: Firebase is a mobile platform, that helps you to quickly develop high-quality apps.

For the push notification implementation using Firebase Cloud Messaging, we will have to log in in the Firebase console. To add Firebase to your Application, you will need a Firebase project and Firebase configuration file.

Steps to follow are mentioned below:

  1. Create a new project in Firebase console.

    project

  2. Name the project in Firebase console.

    console

  3. After creating the project, you will get Firebase overview page. Select “Add Firebase to your Android Application”.

    application

  4. After adding an Application to your Firebase project, enter your Application’s package name, when prompted. Here SHA-1 signing certificate is optional.

    adding

  5. After setting the package name of your Application , you’ll download a Google-Services.json file.

    package

  6. Now, just move the downloaded Google-Services.json into your Android app module root directory. Path : app/. 

    module

  7. Update the root level build.gradle file by adding Google-Services plugin in the dependencies block.

    Update root

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

    plugin

    plugin

  9. Send your first message to the single device (On the basis of FCM Token) or you can broadcast the message to the multiple devices as per your Application's package name.

    • To the group of devices (On the basis of application package name).

      group

    • To the single device (On the basis of FCM token).

      single device

      single device

Source code is given below:

MainActivity.java

  1. package com.abhijeet.firebasenotificationdemo;  
  2. import android.os.Bundle;  
  3. import android.support.design.widget.FloatingActionButton;  
  4. import android.support.design.widget.Snackbar;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.support.v7.widget.Toolbar;  
  7. import android.view.View;  
  8. import android.view.Menu;  
  9. import android.view.MenuItem;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
  17.         setSupportActionBar(toolbar);  
  18.   
  19.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);  
  20.         fab.setOnClickListener(new View.OnClickListener() {  
  21.             @Override  
  22.             public void onClick(View view) {  
  23.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)  
  24.                         .setAction("Action"null).show();  
  25.             }  
  26.         });  
  27.     }  
  28.     @Override  
  29.     public boolean onCreateOptionsMenu(Menu menu) {  
  30.         // Inflate the menu; this adds items to the action bar if it is present.  
  31.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  32.         return true;  
  33.     }  
  34.   
  35.     @Override  
  36.     public boolean onOptionsItemSelected(MenuItem item) {  
  37.         // Handle action bar item clicks here. The action bar will  
  38.         // automatically handle clicks on the Home/Up button, so long  
  39.         // as you specify a parent activity in AndroidManifest.xml.  
  40.         int id = item.getItemId();  
  41.   
  42.         //noinspection SimplifiableIfStatement  
  43.         if (id == R.id.action_settings) {  
  44.             return true;  
  45.         }  
  46.   
  47.         return super.onOptionsItemSelected(item);  
  48.     }  
  49. }  
FirebaseMessagingServiceDemo.java
  1. package com.abhijeet.firebasenotificationdemo;  
  2. import android.app.NotificationManager;  
  3. import android.app.PendingIntent;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.media.RingtoneManager;  
  7. import android.net.Uri;  
  8. import android.support.v4.app.NotificationCompat;  
  9. import android.util.Log;  
  10.   
  11. import com.google.firebase.messaging.FirebaseMessagingService;  
  12. import com.google.firebase.messaging.RemoteMessage;  
  13.   
  14.   
  15. public class FirebaseMessagingServiceDemo extends FirebaseMessagingService {  
  16.   
  17.     private static final String TAG = "FirebaseMsgServiceDemo";  
  18.   
  19.     @Override  
  20.     public void onMessageReceived(RemoteMessage remotemsg) {  
  21.          
  22.         Log.d(TAG, "From -> " + remotemsg.getFrom());  
  23.         Log.d(TAG, "Demo Notification Body -> " + remotemsg.getNotification().getBody());  
  24.         sendNotification(remotemsg.getNotification().getBody());  
  25.     }  
  26.   
  27.   
  28.     private void sendNotification(String messageBody) {  
  29.         Intent intent = new Intent(this, MainActivity.class);  
  30.         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  31.         PendingIntent pendingIntent = PendingIntent.getActivity(this0, intent,  
  32.                 PendingIntent.FLAG_ONE_SHOT);  
  33.   
  34.         Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);  
  35.         NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)  
  36.                 .setSmallIcon(R.mipmap.ic_launcher)  
  37.                 .setContentTitle("Demo Notification")  
  38.                 .setContentText(messageBody)  
  39.                 .setAutoCancel(true)  
  40.                 .setSound(soundUri)  
  41.                 .setContentIntent(pendingIntent);  
  42.   
  43.         NotificationManager notificationManager =  
  44.                 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  45.         notificationManager.notify(0, notificationBuilder.build());  
  46.     }  
  47. }  
FirebaseInstanceIDServiceDemo.java
  1. package com.abhijeet.firebasenotificationdemo;  
  2. import android.util.Log;  
  3. import com.google.firebase.iid.FirebaseInstanceId;  
  4. import com.google.firebase.iid.FirebaseInstanceIdService;  
  5.   
  6. public class FirebaseInstanceIDServiceDemo extends FirebaseInstanceIdService {  
  7.     private static final String TAG = "FirebaseIIDServiceDemo";  
  8.     public String[] name;  
  9.   
  10.     @Override  
  11.     public void onTokenRefresh() {  
  12.         String token = FirebaseInstanceId.getInstance().getToken();  
  13.         System.out.println("my firebase token " + token );   
  14.     }  
  15.     private void sendRegistrationToServer(String token) {  
  16.   
  17.     }  
  18. }  
Output

Output

You can also check the analytics of all the notifications in the Firebase console.

console

 

Up Next
    Ebook Download
    View all
    Learn
    View all