You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin中FirebaseMessagingService接收消息但通知无法显示问题求助

解决Firebase数据通知不显示的问题

看起来你已经能正常接收Firebase的数据载荷,但通知弹不出来,大概率是sendNotification()函数里有几个关键环节没处理到位,我帮你梳理下常见的坑和修复方案:

1. 必须创建Android O及以上的通知渠道

从Android 8.0(API 26)开始,所有通知必须绑定到通知渠道,否则系统会直接忽略通知请求。你可能漏掉了这一步:

private fun createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channelId = "DATA_NOTIFICATION_CHANNEL"
        val channelName = "数据通知渠道"
        val channelDesc = "接收Firebase数据推送的专属渠道"
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        // 创建渠道并注册到系统
        val channel = NotificationChannel(channelId, channelName, importance).apply {
            description = channelDesc
        }
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

建议在Service的onCreate()里调用一次这个方法,或者在sendNotification()开头执行。

2. 补全Intent与PendingIntent的配置

你的代码里intent.addFlags(Intent.FLAG_ACTIVITY_C...没写完,通常需要设置这两个Flag来保证通知跳转的行为符合预期:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)

同时创建PendingIntent时,要适配Android 12+的权限要求,使用正确的Flag:

val pendingIntent = PendingIntent.getActivity(
    this,
    item.id.hashCode(), // 用item的唯一ID当请求码,避免PendingIntent复用冲突
    intent,
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    } else {
        PendingIntent.FLAG_UPDATE_CURRENT
    }
)

3. 完善NotificationCompat.Builder的核心配置

通知的小图标是必填项,如果设置错误或者缺失,通知根本不会显示。同时要关联之前创建的渠道ID:

val notificationBuilder = NotificationCompat.Builder(this, "DATA_NOTIFICATION_CHANNEL")
    .setSmallIcon(R.drawable.ic_notification_small) // 必须用纯色小图标,不能用带透明度的大图
    .setContentTitle(item.title)
    .setContentText(item.content)
    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
    .setContentIntent(pendingIntent)
    .setAutoCancel(true) // 点击通知后自动消失

// 发送通知,用唯一ID区分不同通知
NotificationManagerCompat.from(this).notify(item.id.hashCode(), notificationBuilder.build())

4. 检查系统后台限制

如果你的App被加入了电池优化列表或者后台运行被限制,也会导致通知无法弹出。可以引导用户:

  • 进入App设置,关闭电池优化
  • 允许App的后台运行权限

完整的sendNotification示例代码

把以上环节整合起来,你的函数应该类似这样:

private fun sendNotification(item: Item) {
    // 初始化通知渠道(仅需执行一次)
    createNotificationChannel()

    val intent = Intent(this, ItemNotificationActivity::class.java).apply {
        putExtra(ItemDetailFragment.ARG_ITEM, item)
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
    }

    val pendingIntent = PendingIntent.getActivity(
        this,
        item.id.hashCode(),
        intent,
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
        } else {
            PendingIntent.FLAG_UPDATE_CURRENT
        }
    )

    val notificationBuilder = NotificationCompat.Builder(this, "DATA_NOTIFICATION_CHANNEL")
        .setSmallIcon(R.drawable.ic_notification_small)
        .setContentTitle(item.title)
        .setContentText(item.content)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)

    NotificationManagerCompat.from(this).notify(item.id.hashCode(), notificationBuilder.build())
}

private fun createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channelId = "DATA_NOTIFICATION_CHANNEL"
        val channelName = "数据通知渠道"
        val channelDesc = "接收Firebase数据推送的专属渠道"
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(channelId, channelName, importance).apply {
            description = channelDesc
        }
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

你可以先检查自己的代码有没有漏掉这些环节,尤其是通知渠道和小图标这两个最容易踩的坑。

内容的提问来源于stack exchange,提问作者ِAmin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:36:05