Implementing GCM in Android

By | March 30, 2018

Hi Friends,

In this article I will be talking about how you can implement GCM in Android using FirebaseMessaging Service.

Below are the steps you need to follow…

Create Project in Android Studio

Create a new project in Android Studio. The package name we use here will be used in the Firebase Console.

Create Project in Firebase Console

For GCM to work, we need to add our project in the Firebase Console. You will be asked to enter your package name which you can find in the AndroidManifest.xml file.

Download google-services

Eventually you can download the google-services.json file. Now copy this file in the project root directory.

Build Gradle

Add this in the project level build.gradle

buildscript {
   dependencies {
       compile ''


Add Permissions

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

Add Firebase Services

We have to add the firebase services in the Manifest file.

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

Add Tags

Add this under the application tag

 	<meta-data android:name="" android:resource="@mipmap/ic_launcher" /> 
 	 <meta-data android:name="" android:resource="@color/colorTransparent" /> 
 	 <meta-data android:name="" android:value="@integer/google_play_services_version" />


Implementation – Java Code

Create a class named “MyFirebaseInstanceIDService” and copy these contents into it.
The class will be automatically called by the system and it will generate a Firebase Token which is the unique identifier for that particular device to which the messages will be sent to.

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import co.centroida.notifications.Constants;

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = "MyFirebaseIIDService";

public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);
    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    preferences.edit().putString(Constants.FIREBASE_TOKEN, refreshedToken).apply();

Message Receiver

This class receives the message from Google Cloud. So when a message is sent to GCM it will be delivered to this class.


import android.content.Intent;
import android.util.Log;


import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.i("GCM MessagingService", "GCM Message :: " + remoteMessage.getData());
        String title = remoteMessage.getData().get("title");
        // Show Notification here...


Testing with Postman

If you have postman client, then you can test GCM instantly.

Go to postman and add the below URL

Now add below two headers in Postman

Authorization : key=YOUR_FIREBASE_SERVER_KEY
Content-Type : application/json

For getting the Server Key, you need to go to the firebase console and check the ‘Cloud Messaging‘ tab.

Now in the Body Tab of your request in Postman, add these

      "title":"This is a sample data",
      "message":"This is a sample message",

Note : Make sure you are supplying the correct REG TOKEN, otherwise the message will not be delivered, also each registration token will have some expiration date and
also you will get a new registration token when you reinstall the app.

Leave a Reply

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