android - FirebaseMessagingService 11.6.0 处理 Intent

标签 android firebase android-intent firebase-cloud-messaging

FirebaseMessagingService 有方法 onMessageReceived() 我们应该覆盖来处理通知,但这只是当应用程序处于 Foreground 时工作。

为了即使在应用程序处于后台时也能处理通知,我过去常常覆盖handleIntent,只调用onMessageReceived()

FirebaseMessagingService 11.6.0 中,handleIntent 方法成为 final方法,也就是说,我无法像以前那样覆盖它。

当我的应用程序在 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) {
        ...
    }
}

最佳答案

它不适合任何人覆盖 handleIntent()。这就是为什么它被定为 final 的原因。此外,您会注意到 javadocs 中完全没有它。 - 这是故意的。

如果您想在任何情况下(前台和后台)处理消息,请使用 onMessageReceived() . javadoc对于那个方法说:

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().

这应该适用于数据消息,但不适用于从控制台发送的通知消息。通知消息具有不同的传递行为。请参阅有关 message types 的文档和 how to handle them .

关于android - FirebaseMessagingService 11.6.0 处理 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47308155/

相关文章:

android - 使用实现或 api 时,Firebase 性能配置因 v1.1.2 而失败

android - 收到 GCM 通知时启动应用程序

android - 分享 Intent 不适用于 Facebook Messenger

ListView 上的 Android 查询 where 子句

java - 未找到默认的 RealmConfiguration。调用 setDefaultConfiguration()

android - 未找到已安装的构建工具。安装 Android 构建工具 30.0 版

android - 多个 SQLiteOpenHelper 仅在 Android 上创建第一个表

firebase - StreamBuilder 中的 Where 子句 Google Cloud Firestore

python - Firebase Python 函数 : Configuration

java - 即使数据通过,Android Intent 也是空的