FirebaseMessagingService 11.6.0 HandleIntent [英] FirebaseMessagingService 11.6.0 HandleIntent

查看:151
本文介绍了FirebaseMessagingService 11.6.0 HandleIntent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FirebaseMessagingService具有方法onMessageReceived(),我们应该override处理notifications,但这仅在应用程序位于Foreground中时有效.

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

即使在应用程序处于后台时也要处理notifications,我曾经覆盖了handleIntent,只调用了onMessageReceived().

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

FirebaseMessagingService 11.6.0中,方法handleIntent成为最终方法,也就是说,我不能像以前那样覆盖它.

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

在11.6.0中我的应用程序处于后台时,我应该如何处理通知?

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

推荐答案

它不适合任何人重写handleIntent().这就是为什么它是最终的.此外,您还会注意到 javadocs -这是故意的.

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.

被调用 应用程序在前台.可以检索通知参数 使用getNotification().

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.

这篇关于FirebaseMessagingService 11.6.0 HandleIntent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆