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 :)