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

Android 12&13自定义布局通知不显示问题求助

Android 12/13自定义布局通知无法显示的排查方案

核心问题排查与修复步骤

1. 必须创建通知渠道(Android O+强制要求)

你的代码仅使用了CHANNEL_ID,但未创建对应通知渠道——这是Android 8.0及以上系统通知不显示的核心原因之一。添加渠道创建代码:

@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel() {
    val name = "自定义通知渠道"
    val descriptionText = "用于展示自定义布局的通知"
    val importance = NotificationManager.IMPORTANCE_DEFAULT
    val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
        description = descriptionText
    }
    val notificationManager: NotificationManager =
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.createNotificationChannel(channel)
}

在onCreate中调用该方法,确保构建通知前渠道已创建。

2. 移除不必要的前台服务行为标记

你当前代码设置了setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE),但这是前台服务通知专用标记,普通通知无需设置,直接移除:

val mBuilder: NotificationCompat.Builder =
    NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setStyle(NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(remoteViews)
        // 移除该行配置
        // .setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)

3. 优化自定义布局适配

  • 根容器高度建议改为wrap_content或至少64dp(当前44dp可能在部分系统中被裁剪),符合通知布局规范
  • 文本样式改用系统通知专用样式,避免自定义颜色在深色模式下不可见:
    <TextView
        android:id="@+id/title"
        ...
        style="@style/TextAppearance.Notification.Title" />
    <TextView
        android:id="@+id/text"
        ...
        style="@style/TextAppearance.Notification" />
    

4. 完善权限申请逻辑(Android 13+)

即使手动授予了POST_NOTIFICATIONS权限,代码仍需添加动态申请逻辑,避免未授权场景下通知失效:

private fun checkNotificationPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
            != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.POST_NOTIFICATIONS),
                1001
            )
        }
    }
}

在onCreate中先执行权限检查,再发起通知。

修改后的完整MainActivity代码示例

class MainActivity : AppCompatActivity() {
    private val CHANNEL_ID = "my_channel_01"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        checkNotificationPermission()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel()
        }
        
        showCustomNotification()
    }

    private fun showCustomNotification() {
        val remoteViews = RemoteViews(packageName, R.layout.custom_notification)
        remoteViews.setTextViewText(R.id.title, "Title")
        remoteViews.setTextViewText(R.id.text, "Text for the notification")

        val mBuilder: NotificationCompat.Builder =
            NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setStyle(NotificationCompat.DecoratedCustomViewStyle())
                .setCustomContentView(remoteViews)
                .setCustomBigContentView(remoteViews)

        val notificationManager = getSystemService(
            Context.NOTIFICATION_SERVICE
        ) as NotificationManager
        notificationManager.notify(111, mBuilder.build())
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun createNotificationChannel() {
        val name = "自定义通知渠道"
        val descriptionText = "用于展示自定义布局的通知"
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
            description = descriptionText
        }
        val notificationManager: NotificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }

    private fun checkNotificationPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
                != PackageManager.PERMISSION_GRANTED
            ) {
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(Manifest.permission.POST_NOTIFICATIONS),
                    1001
                )
            }
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 1001) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                showCustomNotification()
            }
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 23:15:57