Google Firebase is a platform providing cloud services for building mobile and web applications, and Firebase Cloud Messaging (FCM) is a part of it. FCM is the new version of Google Cloud Messaging (GCM). It is not necessary to migrate from GCM to FCM as support will still be there – lots of applications on the market still use GCM. But GCM will not receive any new features, all the updates in client side features and functionalities will be added to FCM only.

This article is intended for both those who want to migrate there GCM implementation to FCM as well as for those who want to integrate FCM in there new Android app development.

Google has ensured migrating from GCM to FCM or integrating FCM into your application is very simple.

If you are migrating from GCM to FCM, the first thing for migration is to import the GCM project as an FCM project. For importing, open Firebase console, select Import Google Project, select the existing GCM project from the list of projects and click Add Firebase.

If you are starting fresh, click on Create New Project and follow the steps.

In the Firebase screen, depending upon whether you are developing an Android application or an IOS application, select Add Firebase to your Android app or Add Firebase to your IOS app. After clicking the respective link you will be required to provide a few details for integrating and implementing FCM in your application.

For Android App Development:-

  1. Provide your application package name.
  2. Signing Certificate SHA-1 key.

After adding the above information click on Add App.

After clicking on Add App, the download file dialogue for downloading file named google-services.json containing all the required details for integration will be displayed. Add the downloaded google-services.json file to your Android studio project’s root directory after switching to Project view.

If you are adding the FCM, modify Project level build.gradle and add classpath variable under dependencies to include google services plugin.

include google services plugin

Click Sync Now to Sync modified Build.gradle files.

Modify App-level build.gradle (<project>/<app-module>/build.gradle)

Modify App-level build.gradle

Also add/modify dependencies in App-level build.gradle

add/modify dependencies in App-level build.gradle

Note: You will require Google Play Services Version to be greater than 29 and Google Repository Version to be greater than 28.

Click Sync Now to Sync modified Build.gradle files.

If you are migrating from GCM, you have to remove permissions and GCMReceiver required by GCM from Android Manifest file as FCM do not require any specific permission or broadcast receiver.

Modify our Listener Service if you are migrating (i.e. instead of extending InstanceIDListenerService) or add a new service class which extends FirebaseInstanceIdService class for generating FCM token and add it to Android Manifest as below:-

class for generating FCM token

If you are migrating from GCM to FCM, you will need to remove the explicit generation of registration token as FCM library does this automatically.

When you start your application and your device is connected to network, FCM automatically calls MyInstanceIDListenerServiceonTokenRefresh() method, inside which you can get the registered token by calling FirebaseInstanceId.getInstance().getToken() or calling from any where in your Activity or Fragment.

If you are not connected to internet or no token is yet generated then FirebaseInstanceId.getInstance().getToken() will return null.

Tip: Create a Broadcast receiver with your own action, and from inside onTokenRefresh() send a broadcast with this action to get notified when Token is generated. Then you can call FirebaseInstanceId.getInstance().getToken() to get the generated token which you can send to server for registration.

Migrate GCMListenerService if migrating to FCM or add new service class if adding fresh, extending FirebaseMessagingService. You are required to override onMessageReceived() callback of FirebaseMessagingService for receiving push messages, onMessageReceived() will only be called if your application is in foreground, if in background then notification will be delivered to system tray, clicking that will open the launcher activity. If notification is with data and application is in foreground then onMessageReceived() will be called or else in background then notification will be added to System tray and data payload will be delivered as an extra of the launcher activity intent.

Be sure to add service extending FirebaseMessagingService to android manifest as:

add service extending to android manifest

That’s it. You are now ready to run your application with FCM. Use your Firebase console to send push notifications to your device using the generated Token.

In the next post, we will learn how to Migrate from GCM to FCM in IOS or integrate FCM in your IOS application.

Android app development company experts have just shared guidelines on migrating GCM to FCM. If there are doubts or questions, you can ask them freely.

Migrate from Google Cloud Messaging (GCM) to Firebase Cloud Messaging (FCM) in Android

About The Author

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>