即使应用程序在前台,如何让 FCM 显示通知? [英] How to let FCM display notification even when app is in foreground?

查看:46
本文介绍了即使应用程序在前台,如何让 FCM 显示通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让 FCM 处理设备上的通知(如果不是数据消息).

Let FCM handle notifications on device, if it's not a data message.

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

How can I force Android to show notification using FCM, even when app is in foreground? I don't want to build my own notification.

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

I am sending two types of messages to my app: data message and normal notification message. Data messages are handled in onMessageReceived(), no matter if the App is in background or foreground or killed which is OK. But now I am also sending normal notification messages through Firebase Console, and they are automatically displayed when app is in background but when app is in foreground, onMessageReceived() is called. Here, I would need to somehow tell FCM to show the contents without me having to build the notification.

我尝试过:

@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
        }
}

但这是我自己通过代码进行的处理,我希望 FCM 以某种方式做到这一点.我该怎么做?

But this is my own handling via code and I want FCM to do that somehow. How can I do that?

推荐答案

当应用不在前台时,通知消息由系统处理.这是根据定义,无法改变行为.

When the app is not in the foreground, notification messages are handled by the system. This is by definition and there is no way to change the behavior.

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

If you want to control what is displayed while the app is not in the foreground, you will have to send a data message.

这篇关于即使应用程序在前台,如何让 FCM 显示通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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