Handling Message Notifications

Other topics

Message handling on Android

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

    // TODO(developer): Handle FCM messages here.
    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.
}

Message handling on iOS

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  // If you are receiving a notification message while your app is in the background,
  // this callback will not be fired till the user taps on the notification launching the application.
  // TODO: Handle data of notification

  // Print message ID.
  NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);

  // Print full message.
  NSLog(@"%@", userInfo);
}

Message handling with app in background or killed

Firebase handles notifications differently when the app is in background (killed process) and when in foreground (active).

When your app is in the background, notification messages are displayed in the system tray, and onMessageReceived is not called. For notification messages with a data payload, the notification message is displayed in the system tray, and the data that was included with the notification message can be retrieved from the intent launched when the user taps on the notification. [1]

If the app is in background the service will trigger the notification by default with the title and body in the notification and as mentioned onMessageReceived method will not be trigger. Instead, the the click will open the activity from the Manifest.xml marked with:

<intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

From this point forward you can get your data within this activity's intent:

if (getIntent() != null && getIntent().getExtras() != null) {
      String customString = (String) getIntent().getExtras().get("myStringData");
      Integer customInteger = (Integer) getIntent().getExtras().get("myIntData");
}

Setting icon and icon background color in this case can be done from within Manifest.xml[2]:

<meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/your_drawable_icon" />

<meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/your_color" />

source 1: FCM background handling

source 2: FCM github repository

Contributors

Topic Id: 8888

Example Ids: 15766,15767,28880

This site is not affiliated with any of the contributors.