Firebase Cloud Messaging

Introduction

 
FCM (Firebase Cloud Messaging) is a cross-platform messaging solution that lets you deliver the messages to one or more devices efficiently, without any delay. This article will explain to you how to create an Android application that can receive push notifications. Previously, we were using Google Cloud Messaging(GCM) in Android to receive push notifications. FCM is the same as GCM but the difference is that FCM lets the users send the notification messages to the devices without creating a server-side code. Because FCM already has a console, we can send push notification from there, to all the devices that are registered for our application.
 
First, create a project in the Firebase Console. For this, go to Firebase Console.
 
create
 
Click on CREATE NEW PROJECT.
 
Fill Project Name and Country Name. Now, click CREATE PROJECT.
 
On the next page, click Add Firebase to your Android App.
 
In the Next window, you need to enter the package name for your Android application and the SHA1 finger print. Enter these values and click ADD APP. Then, one file will be downloaded i.e. google-services.json.
 
create
 
Add this JSON file to your App, like the below screenshot:
 
google-services.json
 
Now, add the following dependency in your App's build.gradle file - compile 'com.google.firebase:firebase-messaging:9.2.1'
 
And, you need to add one more line in the same file for Google Services. 
 
apply plugin: 'com.google.gms.google-services'
 
Now, your App's Gradle file will look like the following:
  1. apply plugin: 'com.android.application'  
  2. android {  
  3. // ...  
  4. }  
  5. dependencies {  
  6. // ...  
  7. compile 'com.google.firebase:firebase-messaging:9.2.1'  
  8. }  
  9. // ADD THIS AT THE BOTTOM  
  10. apply plugin: 'com.google.gms.google-services'   
If you want to handle the messages that you receive in the background, then you need to have a service extended from FirebaseMessagingService. The main uses of this file are, receiving notifications in the foregound App, receiving data payloads, sending the upstream messages, and so on. This is why you need to extend this class.
  1. <service android:name=".MyFirebaseMessagingService">  
  2.     <intent-filter>  
  3.         <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter>  
  4. </service>  
  1. <service android:name=".MyFirebaseInstanceIDService">         
  2.      <intent-filter>          
  3.           <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
  4.      </intent-filter>  
  5. </service>    =
For creation, rotation, and updating of the registration token, we need another service that extends from FirebaseInstanceIdService,
 
On the initial start-up of the App, FCM SDK generates a registration token for the client App. This registration token is needed for sending messages to the client applications from the server. In the inTokenRefresh(), we will get the token.
  1. public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService  
  2. {  
  3.     @Override  
  4.     public void onTokenRefresh() {  
  5.         // Get updated InstanceID token.  
  6.         String refreshedToken = FirebaseInstanceId.getInstance().getToken();  
  7.         Log.e("Token""Refreshed token: " + refreshedToken);  
  8.         // TODO: Implement this method to send any registration to your app's servers.  
  9.         /// sendRegistrationToServer(refreshedToken);  
  10.     }  
  11. }  
The service that extends from FirebaseMessagingService lets us do anything we want with the notification message.
  1. public class MyFirebaseMessagingService extends FirebaseMessagingService   
  2. {  
  3.     @Override  
  4.     public void onMessageReceived(RemoteMessage remoteMessage)   
  5.     {  
  6.         // TODO(developer): Handle FCM messages here.  
  7.         // If the application is in the foreground handle both data and notification messages here.  
  8.         // Also if you intend on generating your own notifications as a result of a received FCM  
  9.         // message, here is where that should be initiated. See sendNotification method below.  
  10.         Log.e("Message""From: " + remoteMessage.getFrom());  
  11.         Log.e("Message""Notification Message Body: " + remoteMessage.getNotification().getBody());  
  12.     }  
  13. }  
firebase
 
That's it. So the configuration is done now. Now, we can check this without writing any server-side code. In the Firebase Console, there are a menu Notifications clicking which we can see the message history and status. There, click the NEW MESSAGE button.
 
In that window, fill all the textfields and click SEND MESSAGE. 
 
MESASGE
 

Summary

 
In this article, I discussed Firebase Cloud Messaging and how we can create a client application in Android, for receiving push notifications. And also, how to check this with Firebase Console.


Similar Articles