What’s new in Firebase Cloud Messaging and how to migrate from GCM

Paolo Rotolo
Glucosio Project
Published in
4 min readJun 13, 2016

--

At I/O 2016, Google turned Firebase into an unified mobile platform and migrated to it some existing services service like Google Cloud Platform, adding some new interesting features.

Firebase’s Services (from firebase.google.com)

Firebase covers all that mobile developers need to build, maintain and scale a mobile app on all platforms (even on iOS): from Storage and databases to innovative tools like Remote Config and Test Lab.

Today we’re going to explore Firebase’s Cloud Messaging Platform, based on the precedent Google Cloud Messaging service.

Differences between FCM and GCM

Firebase Cloud Platform inherits GCM’s core infrastructure but simplifies the client development. Developers no longer needs to write their own registration or subscription retry logic. Now, you can integrate FCM in your app with just a few lines of code.

Plus, FCM includes a web console called Firebase Notifications that reminds me of Parse Push Console (RIP, :/).

However, GCM is not deprecated: Google will continue to support it but all new client-side features will be available on FCM SDK only. The protocol remains the same server-side though.

Set Up Firebase Cloud Messaging

Adding FCM to your app is very easy. First add FCM gradle dependence:

dependencies {
compile 'com.google.firebase:firebase-messaging:9.0.2'
}

Then you need to write a service that extends FirebaseMessagingService that will handle notifications when the app is in the background. Another service, FirebaseInstanceIdService, will handle the creation and updating of registration tokens.

<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>

All of this goes in AndroidManifest.xml of course.

As I said before, Firebase creates a token for each client app instance. You’ll need this token if you want to target and send customized messages to specific devices from Firebase’s console. To get this token, you can simply override the method onTokenRefresh and send the updated token to your server each time.

@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();

// Send the new token to your server
sendRegistrationToServer(refreshedToken);
}

Be careful because FirebaseInstanceID.getToken() returns null if the token has not yet been generated.

Now comes the fun part.

Open your Firebase Console and click on the Notifications tab.

Time to send your first message!

Select Send your first message (\o/). You’ll now be able to edit message title and label and select the target, as well as the delivery date. In fact, you can even pick a time and date and send the notification automatically (you can also send the message according to the receiver’s timezone).

You can target your users by language (you’ll also get the estimated number of users for each language) or/and for app version. Selecting audience you’ll be able to target only the users who purchased something on your app or the ones who didn’t.

Finally, you may want to send a message to a specific device: click on “Single Device” and insert the token you received with the method I wrote before.

Then, in the Conversion tab you can track the users that purchased something right after they read the message. As you can see, Cloud Messaging is perfectly integrated with Firebase Analytics to help you monetize.

Other Advanced options include setting the notification priority, the expiration period, and if you want to play a sound when the notification is received :)

We managed to integrate some Firebase services like FCM in Glucosio in just one day, so I’m sure you can do it too in the same time for your apps.

If you’re interested in Glucosio, a free and opensource tool for diabetics, visit this page. We’re always looking for new contributors.

Happy coding and have fun!

--

--

Paolo Rotolo
Glucosio Project

Android Dev @ Blinkist. Lead @GDG Bari. Pursuing Master in Computer Engineering at PoliBa. Big #OpenSource supporter and #Kotlin fan.