6

The FirebaseMessagingService has the method onMessageReceived() which we should override to handle notifications, but this only works when the app is in Foreground.

To handle notifications even when the app is in background, I used to override the handleIntent, to just call the onMessageReceived().

In FirebaseMessagingService 11.6.0, the method handleIntent became final, with that said, I can't override it as I was doing.

How should I handle notifications when my app is in background in the 11.6.0?

public class NotificationsListenerService extends FirebaseMessagingService {
    private static final String TAG = "NotificationsListenerService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) 

        String notifyData = remoteMessage.getData().get("notifData");

        if(notifyData.contains("|")){
            String[] itens = notifyData.split("\\|");
            notifyData = itens[0];
        }


        String notifyType = remoteMessage.getData().get("notifType");
        String title = remoteMessage.getData().get("title");
        String message = remoteMessage.getData().get("body");

        if(!isAppInForeground(App.getContext())){
            sendNotification(title, message, notifyData, notifyType);
        }
    }

    @Override
    public final void handleIntent(Intent intent) {
        ...
        this.onMessageReceived(builder.build());
        ...
    }

    private void sendNotification(String messageTitle, String messageBody, String notifyData, String notifyType) {
        ...
    }


    //Detect if app is in foreground
    private boolean isAppInForeground(Context context) {
        ...
    }
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Guilherme Lima Pereira
  • 1,402
  • 5
  • 17
  • 35

3 Answers3

4

It's not intended for anyone to override handleIntent(). That's why it was made final. Also, you'll notice that it's completely missing from the javadocs - that's intentional.

If you want to handle a message in any circumstance (both foreground and background), use onMessageReceived(). The javadoc for that method says:

Called when a message is received.

This is also called when a notification message is received while the app is in the foreground. The notification parameters can be retrieved with getNotification().

This should work for data messages, but not notification messages sent from the console. Notification messages have different delivery behavior. See the documentation about message types and how to handle them.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 2
    I've read those articles, but when my app is in background, it just receive the notification, but when the user taps on it, the notification is dismissed. I've already developed the logic of click in the notification to direct the user to specific activities. When I used to override the handleIntent, it worked perfectly. – Guilherme Lima Pereira Nov 15 '17 at 16:55
  • Right, that's a "notification" type message sent from the console. Instead, you should be sending a "data" type message from a backend you control to be able to receive it in either the background or the foreground using the same code. – Doug Stevenson Nov 15 '17 at 17:18
  • Thanks, Doug! I'll talk to the backend developer to send me Data message. – Guilherme Lima Pereira Nov 15 '17 at 17:32
  • 7
    This is terrible. We are now unable to upgrade as we have our backend servers sending the same notifications to iOS and android, so all our android push messages have a notification setting. With this change its now impossible for us to render our notifications as desired without the backend system starting to differentiate the messages they send through FCM based on the device receiving the message. – Jake Hall Nov 29 '17 at 23:52
  • 1
    @JakeHall I'm in the exact same situation, cannot use data only message since iOS would not receive in when app is killed. – anni Jan 27 '18 at 22:32
  • 3
    @annihil we ended up having to create our own implementation of a BroadcastReceiver and JobIntentService to receive the FCM messages and explicitly pulling out the intent filters which sent the messages to the FCM classes shipped w/ their SDK – Jake Hall Jan 30 '18 at 11:58
  • 1
    @JakeHall Do you have that Receiver and JobIntentService open-source anywhere? – Matt Mc Apr 02 '18 at 00:42
  • If using only data messages, (not notifications from the Firebase GUI console), onMessageReceived() will always be called, both when app is in the foreground and when app is in the background. If using notification messages from the Firebase GUI console, onMessageReceived() is only called when app is in the foreground. – user1652110 Aug 20 '18 at 04:39
  • For anyone visiting this thread in 2022, recent (as of 2018, anyway) versions of the Firebase SDK allow `handleIntent` to be overridden again. Despite my best efforts I can't find a straight answer from Google/Firebase on *why* the SDK behaves the way it does, so overriding `handleIntent` is still the only way to bypass the frustrating behavior of displaying an automatic notification and *not* calling `onMessageReceived`. – Sapph Apr 01 '22 at 06:56
4

I'd add that in FirebaseMessagingService 11.8.0 docs, it is stated in https://firebase.google.com/docs/cloud-messaging/android/receive that if a notification has a data payload it will call onMessageRecieved() when the app is in the foreground, and if the app is in the background the notification and data payload are delivered in the extras of the intent of your launcher Activity.

So, this means you need to decide how to handle the notification in two places, depending on whether the user is actively using the app or if it is in the background.

As you have seen yourself, if you receive the notification while the app is in the foreground, onMessageReceived() is called and you handle the notification there.

When the app is launched from the background, you have 2 options:

1: By default, the notification is sent to your system tray, and when it is clicked it opens your main activity, passing the data (what would have been remoteMessage.getData() in onMessageReceived()) to your activity as intent extras. You can handle the extras in your main activity like so and decide what to do with them, for instance check for a key value and launch a related intent.

    // [START handle_data_extras]
    if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            Object value = getIntent().getExtras().get(key);
            Log.d(TAG, "Key: " + key + " Value: " + value);
        }
    }
  1. You can decide what intent to open on-click if you add an intent-filter in your app manifest and a designated "click_action" value in your notification, and then handle the intent extras in the designated activity. See https://stackoverflow.com/a/39665485/3746204

I'd also suggest checking the firebase messaging sample app for ideas: https://github.com/firebase/quickstart-android/tree/master/messaging

Alex Conner
  • 240
  • 5
  • 19
2

i have the same problem after update firebase library version.

i think the easiest way is downgrade firebase library again (i use 11.4.2) and handleIntent() still works !

M Moersalin
  • 290
  • 3
  • 12