即使应用程序在前台,如何让 FCM 显示通知?

Pri*_*alj 5 android android-notifications firebase-cloud-messaging

如果不是数据消息,让 FCM 处理设备上的通知。

即使应用程序在前台,如何强制 Android 使用 FCM 显示通知?我不想建立自己的通知。

我正在向我的应用程序发送两种类型的消息:数据消息和普通通知消息。数据消息被处理onMessageReceived(),无论应用程序是在后台还是前台或被杀死都可以。但现在我也通过 Firebase 控制台发送正常的通知消息,当应用程序在后台时会自动显示,但当应用程序在前台时会onMessageReceived()被调用。在这里,我需要以某种方式告诉 FCM 显示内容,而不必构建通知。

我试过:

@Override public void onMessageReceived(RemoteMessage remoteMessage) {
        if(remoteMessage.getData() == null || !remoteMessage.getData().containsKey("specificKey")) {
            // notification msg, let FCM handle it
            super.onMessageReceived(remoteMessage); // this is not working - I want FCM to show notification the same as if the app was in background. 
            return;
        } else {
          // data message, I'm handling it on my own and showing a custom notification, working fine and well
        }
}
Run Code Online (Sandbox Code Playgroud)

但这是我自己通过代码处理,我希望 FCM 以某种方式做到这一点。我怎样才能做到这一点?

Fra*_*len 2

当应用程序不在前台时,通知消息由系统处理。这是根据定义,没有办法改变这种行为。

如果您想控制应用程序不在前台时显示的内容,则必须发送数据消息。