Getting Started with Firebase Cloud Messaging on Android

We’re back to our Getting Started series in an aim to cover all the Firebase services on Android.

As big as Cloud Messaging is, it leaves me to wonder why I didn’t cover this sooner.

 

Cloud Messaging in a Nutshell

Cloud messaging is your standard go-to service for increasing user engagement. You use it to send notifications directly to your user’s (or segments of your users) phones. This can be done in a number of ways

Firebase Cloud Messaging Types

Sending messages through the Firebase Console is the easiest way to get into Cloud Messaging. You build the message manually and set which of your users are to receive the message and it can be sent immediately then and there.

Topic Messaging allows you to send messages to users who have subscribed to a topic. You can send messages to subscribed users either with an HTTP Post request or using the Firebase Console.

Device Group Messaging allows you to send messages to multiple devices of the same user. HTTP Post requests are used for both defining device groups as well as sending messages to them.

Use Cases of Firebase Cloud Messaging

  • Informing users of updates and new features
  • Introducing offers and sales
  • Encouraging visitors to return

These are just a few examples. What you can do with Cloud Messaging can depend largely on the nature of your app.

First Step into Cloud Messaging

Without a doubt, the easiest way to use Firebase Cloud Messaging is sending messages through the Firebase Console. So that’s what we’re covering here while Topic and Device Group Messaging will be their own articles in the near-future.

Before you can start sending messages, we’ll have to prepare our app for receiving messages.

Import the Dependency

implementation 'com.google.firebase:firebase-messaging:17.3.0'

Add this to your app-level build.gradle file.

Then create a service that extends FirebaseMessagingService which will handle receiving your messages. It just being there is the minimum you need.

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

Declare your newly created service in your AndroidManifest.xml file.

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
     See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
     notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

OPTIONALLY you can add these to your Manifest as well to set a default custom icon and color for incoming notifications from your messages.

Now, your app should be able to receive messages. However, if you want to target single devices or create device groups, you’ll need to access your registration token.

 

Retrieve Current Registration Token

In your app, create a class that extends FirebaseInstanceIdService which has access to the onTokenRefresh method.

@Override
public void onTokenRefresh() {
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + token);
}

After retrieving this token, you can send it to your server or store it however you want. Just note that this token can change more than once for a single user.

 

Sending Messages with the Console

In the Firebase Console, go to Cloud Messaging and there you can use the Notifications Composer.

 

Using this, you can create your message, schedule it, and set its target users. You’ll need a user’s registration token to send a message to a Single Device.

Using User Segment, you can send your message to all the users of your app or to segments of them defined by properties such as Version, Language, or custom properties defined in-app. For more on this, check out my User Segmentation series.

Read More

Check out the other parts of my Firebase Cloud Messaging series:

2. Topic Messaging with Firebase Cloud Messaging on Android

3. Device Group Messaging with Firebase Cloud Messaging

4. Constructing POST HTTP Firebase Cloud Messages for dummies and YOU

Subscribe to the Newsletter