The Best Tutorial to Learn Firebase Cloud Messaging (FCM) Integration in Android App Development


Let us take up a subject that is been trending across the Android app development niche i.e., Firebase Cloud Messaging (FCM). Google has recently released an improved version of GCM which is named as FCM (Firebase Cloud Messaging). Before we start, let me tell you that this blog is for pure technical practice. I have had a great experience working on it and for your info, I have added some frequently asked questions about FCM & GCM.

  1. What exactly is Firebase Cloud Messaging?
  • Firebase Cloud Messagingis a powerful API that lets you deliver push messages reliably and independent of the platform you are developing on. The developers can notify the users that new data is available for sync and send notification messages. FCM comes with built-in targeting and analytics.
  • We can use Firebase Cloud Messagefor sending an instant message of up to 4KB in size.
  1. Why to use Firebase Cloud Messaging instead of Google Cloud Messaging (GCM)?
  • Firstly, GCM is already depreciated & might be in next coming days GCM is going to fully depreciated. Firebase Cloud Messaging is an improved version of the Google Cloud Messaging API under the brand of Firebase.
  • You can also update your old GCM project as a Firebase project.

Step to integrate FCM

 

 

  • Add Name of Project & Country then click on “Create Project”


  • Now you will see “Add Firebase to your Android app” then add project package details (Mandatory) & Debug signing certificate SHA-1 key (Optional)

 

 

  • After completing this step it will download google-services.json file which we have to add in our Android project.

  • We need to add the Internet permission & Vibrate permission in AndroidManifest.xml file to generating a notification alert on the android device with Internet.

AndroidManifest.xml

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

<uses-permission android:name=”android.permission.VIBRATE”/>

 

  • Add an entry for service which extends FirebaseMessagingService to handle the message after receiving notifications on apps in the background. This is very important to receive android push notifications.

AndroidManifest.xml

<service

android:name=”.MyAndroidFirebaseMessagingService”>

<intent-filter>

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

</intent-filter>

</service>

 

  • Add an entry for service which extends FirebaseInstanceIdService is used to handle the registration token.

AndroidManifest.xml

<service

android:name=”.MyAndroidFirebaseInstanceIDService”>

<intent-filter>

<action android:name=”com.google.firebase.INSTANCE_ID_EVENT”/>

</intent-filter>

</service>

  • Now open your project’s gradle and add the following dependency.

build.gradle

classpath ‘com.google.gms:google-services:3.0.0’

  • Now open your app’s gradle and add apply plugin with dependencies in the dependency.

build.gradle

apply plugin: ‘com.google.gms.google-services’

dependencies

compile ‘com.google.android.gms:play-services:9.2.0’

compile ‘com.google.firebase:firebase-messaging:9.2.1’

 

  • Create a java class which will extend FirebaseInstanceIdService which will handles creation & updating of registration tokens.

Java Class

public class FCMInstanceService extends FirebaseInstanceIdService {

 

private static final String TAG = “MyAndroidFCMIIDService”;

 

@Override

public void onTokenRefresh() {

//Get hold of the registration token

String refreshedToken = FirebaseInstanceId.getInstance().getToken();

//Log the token

Log.d(TAG, “Refreshed token: ” + refreshedToken);

}

private void sendRegistrationToServer(String token) {

//Implement this method if you want to store the token on your server

}

}

 

The onTokenRefresh() callback fires on generation of a new registration token.

 

  • Create a java class which will extend FirebaseMessagingService which handles message in the background and sends the push notification.

Java Class

public class MyAndroidFirebaseMsgService extends FirebaseMessagingService {

private static final String TAG = “FCM-Service”;

@Override

public void onMessageReceived(RemoteMessage remoteMessage) {

Log.d(TAG, “From: ” + remoteMessage.getFrom());

Log.d(TAG, “Notification Message Body: ” + remoteMessage.getNotification().getBody());

//To create notification

createNewNotification(remoteMessage.getNotification().getBody());

}

 

private void createNewNotification( String messageBody) {

Intent intent = new Intent( this , ResultActivity. class );

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,

PendingIntent.FLAG_ONE_SHOT);

 

Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)

.setSmallIcon(R.mipmap.ic_launcher)

.setContentTitle(“Android Tutorial Point FCM Tutorial”)

.setContentText(messageBody)

.setAutoCancel( true )

.setSound(notificationSoundURI)

.setContentIntent(resultIntent);

 

NotificationManager notificationManager =

(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0, mNotificationBuilder.build());

}

}

 

After receiving message onMessageReceived() get called & createNewNotification() method will create a push notification

 

  • Now app is done, we will test the app by sending a push notification from Firebase Notifications panel using Firebase Consol. Please follow steps shown in images.

After this one you will see you have got a push notification.

 

The tutorial is an example of how to use FCM i.e. Firebase Cloud Messaging in Android apps. We have configured the json file by creating a project on firebase consol. Using registration token we can send push notification easily to single device or multiple devices. This is one of the best features of Android app development.

 

Happy Coding.

Share this: