From GCM to FCM : The “Why and How” of Cloud Messaging Apps for Android

Wassa Team
Wassa
Published in
7 min readOct 30, 2018

Note : This paper will focus about the process for Android apps

TLDR : If you have an android app using the google cloud messaging service the deprecation has been announced which makes the switching necessary to Firebase

As of April 10, 2018, Google has deprecated GCM. The GCM server and client APIs are deprecated and will be removed as soon as April 11, 2019. Migrate GCM apps to Firebase Cloud Messaging (FCM), which inherits the reliable and scalable GCM infrastructure, plus many new features.

With the latest updates of phone systems , user needs and apps it became more than obvious that these last have to be less power and processor consuming. One of the solutions is to make the app dormant and the push notification were created to help on this purpose.

Push Notifications

Web push notifications are clickable rich content messages, sent to your device by a website or a web app Push notifications are delivered to your mobile device or desktop, even when your app is closed or in background .

These notifications can only be sent to users who have subscribed to these notifications from a specific website on Chrome, Firefox and Safari As these notifications are pushed to user’s device, users don’t have to be present on the specific website to receive these notifications.

Push Notifications are best used for delivering time bound content to engage users. Some of the popular use cases include retargeting users abandoning the cart, activating dormant users with offers, retaining loyal readers with personalized content and more.

Here we’re gonna focus on two tools which actually are kinda the same tools just FCM is meant to become more evolved than GCM . And this will require before all to have a Firebase account

The Concept

We want to send a notification to all devices that have your app installed whenever a particular event is triggered. How are other devices going to detect this event? Easy, you need to have a server that will keep listening for this event and as soon as it occurs, this server will notify all the other devices and voila, you can now create notifications! The easiest way to accomplish this is by using Firebase Cloud Messaging(FCM).

Step 1: Adding your app to FCM

1. Go to console.firebase.google.com and login to your account.

2. Click on “Add Project”.

3. Enter your Project name, select your region and click on “Create Project”.

4. After the project is created, click the settings “gear” icon in the upper-left and select “Project settings”.

5. Under Project settings, click on “Add Firebase to your Android app”.

6. Fill the Android package name, nickname (optional) and click on “Register app”.

Note: This package name should be similar to the one used in the mag+ publish portal

7. Download .json file and click Next. Similarly, click Next on the next step and skip the last step to complete the process.

8. On the screen that appears, Click the “Cloud Messaging” tab.

9. On this page, you will find your “Server key” (API key)

Step 2: Creating your FCM Api publish key

Before You Begin:

  • Make sure you have copied the Server key (API key) from the Firebase Console.
  • Make sure you have downloaded the .json file from the Firebase Console.

1. Login to the mag+ Publish portal and go to Apps > Android > Build Settings.

2. Under “Google Cloud Messaging,” click the “enable notifications via FCM” checkbox, paste the API Key in the appropriate field, and upload the .json file.

3. Click the “Save” button at the bottom of the page.

Step 3 : Linking FCM in the Google Play Developer Console

Before You Begin:

1. In the screen that appears, click on Development tools > “Services & APIs” link on the left. On the page that displays, click on the “Link a Sender ID” button.

2. In the window that displays, paste the “API Key” (copied previously) into the “FCM API Key” field and click the “Link” button.

Note: If the “Link” button is greyed out you may need to click off of the input field or tab out of it for it to become active.

3. You should now see a message saying that the Sender ID has been successfully linked and see it appear under “LINKED SENDER IDS”.

Congratulations! You have now successfully enabled Google Cloud Messaging (Firebase) and linked it to your Android app.

Now we can update our GCM code to switch onto the FCM service

Step 4 : Switching your app code from GCM to FCM

1. Import your GCM project as a Firebase project

The instructions in this section assume that you are adding Firebase to an existing Google Cloud project for GCM. If you already have an existing Firebase project into which you would like to migrate a GCM app and its users, see Migrate GCM to an existing Firebase project for important alternative instructions.

In the Firebase console, select Add Project.

Select your GCM project from the list of existing Google Cloud projects, and select Add Firebase.

In the Firebase welcome screen, select Add Firebase to your Android App.

Provide your package name and SHA-1, and select Add App. A new google-services.json file for your Firebase app is downloaded.

Select Continue and follow the detailed instructions for adding the Google Services plugin in Android Studio.

Note: If Google creates an additional server key for your project labeled, “(auto created by Google Service),” use this new key instead of your existing key.

2. Switch to FCM in the app-level build.gradle

Before:

After:

3. Edit your app’s manifest

The FCM SDK automatically adds all required permissions as well as the required receiver funtionality. Make sure to remove the following obsolete (and potentially harmful, as they may cause message duplication) elements from your app’s manifest:

Remove from AndroidManifest.xml because it will be managed implicitly by firebase

… and finally your push notification handler

4. Edit your app’s notification handler

…will become

… and in it’s complete class gives us this

…and that’s it for the app side

5. Update server endpoints

Update your server code to use new FCM endpoints for sending messages via HTTP and XMPP. Note that the new FCM version of gcm-http.googleapis.com/gcm/ is fcm.googleapis.com/fcm/ (without “http”):

FCM supports HTTP and XMPP protocols that are virtually identical to the GCM server protocols, so you don’t need to update your sending logic for the migration. Optionally, you may want to evaluate the FCM HTTP v1 API, which offers an improved security model and new capabilities for customizing messages across platforms.

Conclusion

With the work of firebase company alongside google services this switching from GCM to FCM is very easy and costs more code removing than adding which for a developer is nice and eases you app life promising better notification app messaging service and better management on the back side .

Do you want to know more about WASSA?

WASSA is an Innovative Digital Agency expert in Indoor Location and Computer Vision. Whether you are looking to help your customers to find their way in a building, enhance the user experience of your products, collect data about your customers or analyze the human traffic and behavior in a location, our Innovation Lab brings scientific expertise to design the most adapted solution to your goals.

Find us on:

· Facebook and Twitter

· LinkedIn

· GitHub

· Our WebSite

--

--

Wassa Team
Wassa
Editor for

Wassa is a company specialized in the design of innovative digital solutions with great added value.