FirebaseMessagingService 11.6.0 HandleIntent

Gui*_*ira 5 android android-intent firebase firebase-cloud-messaging

FirebaseMessagingService有方法onMessageReceived(),我们应该哪个override来处理notifications,但是这只能在应用程序中Foreground.

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

FirebaseMessagingService11.6.0中,该方法handleIntent成为最终的,据说,我无法像我一样覆盖它.

当我的应用程序在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) {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 4

它不适合任何人覆盖handleIntent()。这就是为什么它被定为最终的原因。另外,您会注意到javadoc中完全缺少它- 这是故意的。

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

收到消息时调用。

当应用程序位于前台时收到通知消息时也会调用此函数。可以使用 getNotification() 检索通知参数。

这应该适用于数据消息,但不适用于从控制台发送的通知消息。通知消息具有不同的传递行为。请参阅有关消息类型以及如何处理它们的文档。

  • 这很糟糕。我们现在无法升级,因为我们的后端服务器向 iOS 和 Android 发送相同的通知,因此我们所有的 Android 推送消息都有通知设置。通过此更改,如果后端系统不开始根据接收消息的设备来区分通过 FCM 发送的消息,我们现在就无法根据需要呈现通知。 (7认同)
  • @annihil,我们最终必须创建自己的 BroadcastReceiver 和 JobIntentService 实现来接收 FCM 消息,并显式拉出将消息发送到随其 SDK 一起提供的 FCM 类的意图过滤器 (3认同)
  • 我已经阅读了这些文章,但是当我的应用程序在后台时,它只会收到通知,但是当用户点击它时,通知就会被忽略。我已经开发了通知中的单击逻辑,以引导用户进行特定活动。当我用来重写handleIntent时,它工作得很好。 (2认同)