Set up a Firebase Cloud Messaging - Android

Find out how to add Firebase Messaging to your App and regularly check if your notification token is up to date.
  1. Set up Firebase and the FCM SDK
  2. Create a class Messaging Service
  3. Edit your app manifest
  4. Access the device registration token
  5. Monitor token generation
  6. Receive messages
  7. Customize notifications

Sending notification to mobile app means using a dedicated service to do so: Firebase. Let’s go through the settings associated to Firebase you’ll need to implement in your app.

Set up Firebase and the FCM SDK

  1. If you haven’t already, add Firebase to your Android project.
  2. In your project-level build.gradle file, make sure to include Google’s Maven repository in both your buildscript and allprojects sections.
  3. Add the dependency for the Cloud Messaging Android library to your module (app-level) Gradle file (usually app/build.gradle):
    implementation ''

Create a class Messaging Service

Once the library is installed, you will implement a service that will listen to notification messages sent by Firebase. For that, create a class WoosmapMessagingService.

public class WoosmapMessagingService extends FirebaseMessagingService {
    public void onMessageReceived(RemoteMessage remoteMessage) {
        WoosmapMessageBuilderMaps messageBuilder = new WoosmapMessageBuilderMaps(this, MainActivity.class);
        WoosmapMessageDatas messageDatas = new WoosmapMessageDatas(remoteMessage.getData());
        if (messageDatas.isLocationRequest () && messageDatas.timestamp != null) {

Edit your app manifest

Once created, WoosmapMessagingService has to be added to you manifest to extend the FirebaseMessagingService. Doing so you will be able to handle notifications in the background and the foreground with custom payloads.

<service android:name=".ExampleInstanceIdService">
    <action android:name="" />
<service android:name=".ExampleMessagingService">
    <action android:name="" />

ExampleInstanceIdService and ExampleMessagingService are your own services which have to inherit from FirebaseInstanceIdService and FirebaseMessagingService.

Access the device registration token

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you’ll need to access this token by extending FirebaseMessagingService and overriding onNewToken.

This section describes how to retrieve the token and how to monitor changes to the token. Because the token could be modified after initial startup, you are strongly recommended to retrieve the latest updated registration token.

The registration token may change when:

  .addOnCompleteListener(new  OnCompleteListener<InstanceIdResult>()  {
    public  void onComplete(@NonNull  Task<InstanceIdResult> task)  {
      if  (!task.isSuccessful())  {  
          Log.w(TAG,  "getInstanceId failed", task.getException());  
      // Get new Instance ID token  
      String token = task.getResult().getToken();
      // Log and toast  
      String msg = getString(R.string.msg_token_fmt, token);
      Log.d(TAG, msg);  Toast.makeText(MainActivity.this, msg,  				

Monitor token generation

The onNewToken callback fires whenever a new token is generated.

* Called if InstanceID token is updated. This may occur if the security of  
* the previous token had been compromised. Note that this is called when the InstanceID token  
* is initially generated so this is where you would retrieve the token.  
public  void onNewToken(String token)  {  
    Log.d(TAG,  "Refreshed token: "  + token);  
    // If you want to send messages to this application instance or  
    // manage 	this apps subscriptions on the server side, send the  
    // Instance ID token to your app server. 

Receive messages

Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you’ll need to write code to handle the onMessageReceived callback. For an explanation of the difference between notification and data messages, see Message types.

Customize notifications

If you want to define the Activity which will be opened when a user clicks on the notification, you first have to set an Uri to this Activity in the Manifest.xml (example for the uri: sample://notif on the MainActivity)

<activity android:name=".MainActivity">
        <data android:scheme="sample"
            android:host="notif" />

Finally you have to define the default Uri which will be opened when the user clicks on the notification in the tag application

<application ... >
    <meta-data android:name="woosmap_notification_defautl_uri" android:value="sample://notif" />

Define the notification’s icon

If you want to customize the small icon displayed in the notification, you must add the icon file in the directory res/drawable and add the following meta-data in the AndroidManifest.xml`

<application ...>
android:resource="@drawable/your_custom_icon_24dp" />