FirebaseMessagingService 11.6.0 HandleIntent

2019-02-17 18:46发布

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条回答
贪生不怕死
2楼-- · 2019-02-17 19:27

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 !

查看更多
祖国的老花朵
3楼-- · 2019-02-17 19:40

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

查看更多
对你真心纯属浪费
4楼-- · 2019-02-17 19:49

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.

查看更多
登录 后发表回答