Firebase Cloud Messaging

Mateus Batista
4 min readApr 19, 2018

--

Hi, my name is Mateus Batista, I’m a Computer Science undergraduate and Android developer lover. “Teaching is the best way to learn.” With this phase in mind, I will start series of article about my apprenticeship with Android and as a result, I will train my English. If you have any comments or suggestion, hit me.

I will start this series with Firebase Cloud Messaging because I had figured out how to migration GCM to FCM and its integration with AWS.

Firebase Cloud Messaging

Firebase cloud Messaging is a cross-platform solution permit you notify the users about new data or send notification messages.

Firebase Console

First, we need to add a new project on Firebase Console, go to console.firebase and start a new project. The documentation has a good tutorial for adding your Android project to a Firebase and I will skip this part, but if you’re interested, just follow the steps.

https://firebase.google.com/docs/android/setup

Add Permissions and Dependencies

We need two permissions, Internet for the AWS delivering the Push and Vibrate for we could generate a notification alert on the Android device.

Open your AndroidManifest.xml file and add these two permissions, as the code below.

Now open your project’s build.gradle and add this dependence.

dependencies {
implementation ‘com.google.firebase:firebase-messaging:9.2.1’
}

I forget to mention, we have to add two main classes in the Manifest too, they are a service and we are going to use them later.

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

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

Understanding the main classes and extensions

FirebaseInstanceIdService

The documentation says “Base class to handle Firebase Instance ID token refresh events.”, in another word, every time the user install or reinstall the app, one token is created, and this class handles this situation.

I used “FirebaseInstanceId.getInstance().getToken()” to get the new token and put in a String, just for example.

If you wanna save the token or send it to your server, you can do this inside “onTokenRefresh()” method, like the code below.

FirebaseMessagingService

The name of the class suggests what it does. Here we receive the message from a Push Notification and handle it. “…It also provides functionality to automatically display notifications…”

We are gonna use the @Override method name OnMessageReceived and this method has a parameter RemoteMessage which is our message.

Now we verify if the message is empty and extract it, then create the push for users.

if (remoteMessage.getData().size() > 0) {

String messageString = remoteMessage.getData().get("message");
}

Let’s do it like a Hannibal, by doing in parts. First, we need to define the PendingIntent, who is the class that is called when the user clicks in a push. I created a customs, sound, icon and color.

Intent it = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), it, 0);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
int ico_notification = R.drawable.starkicon;
int color = ContextCompat.getColor(this, R.color.starkColor);

To create notifications you use the NotificationManager class.

NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);

Before we move on, Android 8 has a singular attention, when Android Oreo came out, it is needed to create and add a NotificationChannel for notification.

String CHANNEL_ID = "FCM_channel_01";
CharSequence name = "Channel fCM";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mNotificationManager.createNotificationChannel(mChannel);
}

At the last part, I used NotificationCompat to build and customize the Push and set in a Notification.

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this,CHANNEL_ID)
.setSmallIcon(ico_notification)
.setContentTitle(getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageString))
.setSound(soundUri)
.setColor(color)
.setAutoCancel(true)
.setVibrate(new long[]{1000, 1000})
.setContentText(messageString);

mBuilder.setContentIntent(contentIntent);
Notification notification = mBuilder.build();

mNotificationManager.notify(0, notification);

FirebaseInstanceIdReceiver

If some of you are familiar with GCM, probably had implement WakefulBroadcastReceiver from GCM. In a Firebase this class is automatically added for you.

I just put this class hare for curiosity.

Testing our application

Go to Firebase Console and just follow the steps below.

Last notes

The role project is in the gitHug, in this link. Thx a lot and one more time, if you have any suggestions about my English or this tutorial, send me a message.

https://github.com/MateusLB/FirebaseCloudMessaging

--

--