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

Android自定义FCM通知不显示求助(Gradle升级后)

Android自定义FCM通知升级Gradle依赖后不显示的排查方案

原本正常显示的自定义FCM通知,在升级所有Gradle依赖库后不再展示,代码已执行且无报错,自定义通知实现了小、大两种布局,核心代码如下:

private fun showCustomPushNotification(message: RemoteMessage, chargerId: String) {
    val notificationLayout = RemoteViews(
        packageName,
        R.layout.pgin_requires_approval_notification_small
    )
    val notificationLayoutExpanded = RemoteViews(
        packageName,
        R.layout.pgin_requires_approval_notification_large
    )

    val title = message.data[MSG_TITLE]
    val subTitle = message.data[MSG_SUB_TITLE]

    notificationLayout.setTextViewText(R.id.tvTitle, title)
    notificationLayout.setTextViewText(R.id.tvSubTitle, subTitle)

    notificationLayoutExpanded.setTextViewText(R.id.tvTitle, title)
    notificationLayoutExpanded.setTextViewText(R.id.tvSubTitle, subTitle)

    val intent = Intent(this, MainActivity::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }

    val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)

    val customNotification = NotificationCompat.Builder(
        this,
        CarInfoProcessingService.APPROVE_EACH_PGIN_NOTIFICATION_CHANNEL_ID
    )
        .setSmallIcon(R.mipmap.ic_launcher)
        .setStyle(NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(notificationLayout)
        .setCustomBigContentView(notificationLayoutExpanded)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
        .setPriority(NotificationCompat.PRIORITY_MAX)
        .build()

    val approveIntent = Intent(this, CustomNotificationListener::class.java)
    approveIntent.putExtra("onClickListener", "approve")
    approveIntent.putExtra("chargerId", chargerId)
    val pendingApproveIntent = PendingIntent.getBroadcast(
        this,
        0,
        approveIntent,
        PendingIntent.FLAG_IMMUTABLE
    )
    notificationLayoutExpanded.setOnClickPendingIntent(R.id.btnApprove, pendingApproveIntent)

    val denyIntent = Intent(this, CustomNotificationListener::class.java)
    denyIntent.putExtra("onClickListener", "deny")
    denyIntent.putExtra("chargerId", chargerId)
    val pendingDenyIntent = PendingIntent.getBroadcast(
        this,
        1,
        denyIntent,
        PendingIntent.FLAG_IMMUTABLE
    )

    notificationLayoutExpanded.setOnClickPendingIntent(R.id.btnDeny, pendingDenyIntent)

    val notificationManager =
        getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.notify(CustomNotificationListener.WHAT_NOTIFICATION_ID, customNotification)
}

以下是具体排查方向:

  • 检查通知渠道有效性:
    Android O及以上版本必须创建对应ID的NotificationChannel,升级依赖后需确认渠道创建逻辑是否正常。确保渠道的importance设置为IMPORTANCE_HIGH或更高,否则通知可能被系统静默。同时检查渠道是否被用户手动关闭。

  • 修复PendingIntent配置与代码顺序:

    1. 当前代码先构建Notification对象,再给RemoteViews设置点击事件,这会导致点击事件无法生效到最终展示的通知中。必须先完成RemoteViews的所有配置(包括点击事件),再调用build()创建Notification。
    2. 尝试给PendingIntent添加FLAG_UPDATE_CURRENT标志,避免因Intent复用导致Extra数据异常,如:PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE。
  • 验证RemoteViews布局兼容性:
    升级依赖后,RemoteViews对控件的支持可能有变化,检查自定义布局是否使用了不兼容的控件(如自定义View)或属性。可先替换为仅含系统原生TextView、Button的极简布局测试,确认是否能正常显示。

  • 检查通知权限与系统限制:
    Android 13及以上版本需要显式申请POST_NOTIFICATIONS权限,需在Manifest中声明并动态请求。同时检查应用的通知权限是否被系统或用户禁用。

  • 调整通知图标与优先级设置:
    避免使用mipmap目录下的图标作为通知小图标,建议使用drawable目录下的透明背景通知专用图标。另外,Android O及以上版本中,setPriority(PRIORITY_MAX)的优先级设置会被渠道的importance覆盖,需确保渠道importance配置正确。

  • 添加日志排查细节:
    打印title、subTitle的取值,确认数据不为空;查看系统日志(过滤关键词NotificationManager),是否存在系统拦截通知的相关日志;打印notificationManager.notify的执行状态,确认通知是否被成功提交。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 10:20:44