首页 > 解决方案 > 在android中显示数字而不是消息的前台通知

问题描述

我正在从Firebase 控制台测试推送通知

当我的应用程序处于后台时,我成功收到了通知

在此处输入图像描述

但是在前台,无论我发送的通知消息显示给我什么消息都是这样的:

在此处输入图像描述

我不知道这个数字是从哪里来的。

下面是我创建通知的代码:

     override fun onMessageReceived(p0: RemoteMessage) {
    super.onMessageReceived(p0)

    p0.notification?.let {
        val notif = NotificationCompat.Builder(this, "12345")
            .setContentTitle(p0.from)
            .setContentText(p0.to)
            .setSmallIcon(R.drawable.ic_menu_send)
            .build()
        val manager = NotificationManagerCompat.from(applicationContext)
            .notify(0, notif)
    }
}

以下是Firebase 控制台的屏幕截图:

在此处输入图像描述

有人可以告诉我如何正确接收我的消息。

更新:还有为什么后台和前台通知 的这种差异

提前致谢。

标签: androidfirebasefirebase-cloud-messaging

解决方案


您正在发送通知消息。您可以按以下方式获取标题和正文

override fun onMessageReceived(p0: RemoteMessage) {
    super.onMessageReceived(p0)

    val title = message.notification.title
    val body= message.notification.body

    p0.notification?.let {
        val notif = NotificationCompat.Builder(this, "12345")
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ic_menu_send)
            .build()
        val manager = NotificationManagerCompat.from(applicationContext)
            .notify(0, notif)
    }

你可以在这里获得更多信息


推荐阅读