FCM Push Notifications: Receive Messages in an Andriod App

A step-by-step guide to receive FCM push notification message in and Android app.


Firebase Receive Message

Welcome to last part of Android Push Notifications using Firebase tutorial. In previous articles, we have discussed following:

If you have not yet completed Android app or app server integration with FCM, I strongly advise to follow above articles before proceeding with this.

Handling Messages in Android App

Firebase notifications may act differently depending on the foreground/background state of the receiving app. To receive FCM push notification messages in foregrounded apps, use a service that extends FirebaseMessagingService. Your service should override the onMessageReceived callback, which is provided for most message types, with the following exceptions:

  • Notifications delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
  • Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

In nutshell, we have following:

App stateNotificationDataBoth
ForegroundonMessageReceivedonMessageReceivedonMessageReceived
BackgroundSystem trayonMessageReceivedNotification: system tray
Data: in extras of the intent.

Edit the app manifest

To use FirebaseMessagingService, you need to add the following in your app manifest:

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

Add these lines inside the application tag to set the custom default icon and custom color:

<!-- 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" />

Android displays the custom default icon for

  • All notification messages sent from the Notifications console.
  • Any notification message that does not explicitly set the icon in the notification payload.

Android uses the custom default color for

  • All notification messages sent from the Notifications console.
  • Any notification message that does not explicitly set the color in the notification payload.

If no custom default icon is set and no icon is set in the notification payload, Android displays the application icon rendered in white.

Override onMessageReceived

By overriding the method FirebaseMessagingService.onMessageReceived, you can perform actions based on the received RemoteMessage object and get the message data:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // ...

    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

Handle notification messages in a backgrounded app

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

Need further help? If you’re confused or facing any issue while completing first step, let’s know in comments.

Love What You're Reading?
Subscribe to our top stories
Subscribe
Give it a try, you can unsubscribe anytime.

Like it? Share with your friends!

attacomsian

A passionate programmer, domainer, writer and photographer! Love writing about web development, latest trends in technology and domain name industry. Working hard to make the world a better place! Follow me at Twitter @attacomsian or visit my website www.attacomsian.com
DON’T MISS OUT!
Subscribe To Our Newsletter
Be the first to get latest updates and exclusive content straight to your email inbox.
STAY UPDATED
Give it a try, you can unsubscribe anytime.
close-link
Love What You're Reading?
Subscribe to our top stories
Subscribe
Give it a try, you can unsubscribe anytime.
50% Off on all advertisement packages. Limited time only!
ORDER NOW