FirebaseMessagingService 11.6.0 HandleIntent

Viewed 13825

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) {
        ...
    }
}
3 Answers

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.

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 !

Related