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

Android 12自定义通知不显示求助(Android 9可正常运行)

Android 12自定义通知不显示问题修复方案

核心问题分析

你的代码在Android 9正常但Android 12失效,主要是因为Android 12对RemoteViews的兼容性检查更严格,同时自定义布局存在不支持的属性,另外新版本系统对通知的权限、视图状态适配有额外要求。


具体修复步骤

1. 修复自定义布局中的不支持属性

你的布局里给LinearLayout设置了app:cardCornerRadius,但RemoteViews不支持LinearLayout使用该属性(此属性属于CardView专属),Android 12会直接加载失败导致通知不显示。

修改后的布局示例:

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/greenCrusoe"
    app:cardCornerRadius="@dimen/common_max_margin_padding_value">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/greenCrusoe">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_icon_checkcheck_white"
            android:layout_gravity="center"
            android:layout_marginTop="@dimen/common_margin_value" />

        <TextView
            android:id="@+id/tv_successfully_notification"
            style="@style/textViewTitleStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingVertical="@dimen/common_min_margin_value"
            android:layout_gravity="center"
            android:drawablePadding="@dimen/common_min_margin_value"
            android:text="@string/not_changed_pin_success_text"
            android:textAlignment="center"
            android:textColor="@color/white"
            android:textSize="15sp" />
    </LinearLayout>
</androidx.cardview.widget.CardView>

2. 适配Android 12的通知视图状态

Android 12及以上通知有折叠/展开两种状态,仅设置setCustomContentView无法覆盖所有场景,需补充设置展开状态的视图:

private fun createNotification(context: Context, activity: Activity, layout: Int, channelId: String ): Notification {
    val remoteView = RemoteViews(context.packageName, layout)
    return NotificationCompat.Builder(context, channelId)
        .setCustomContentView(remoteView)
        .setCustomBigContentView(remoteView) // 新增:适配展开状态
        .setSmallIcon(R.drawable.ic_logo_alfred_sin_texto)
        .setColor(ContextCompat.getColor(context, R.color.greenCrusoe))
        .setAutoCancel(true)
        .setPriority(NotificationCompat.PRIORITY_MAX)
        .build()
}

3. 补充Android 13+的通知权限检查

Android 13(API 33)开始需要单独申请POST_NOTIFICATIONS权限,否则通知无法弹出:

fun executeNotification(
    context: Context, notificationId: Int, activity: Activity, layout: Int, channelId: String) {
    // Android 13+ 检查通知权限
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.POST_NOTIFICATIONS), 1001)
            return
        }
    }
    val notificationManager = NotificationManagerCompat.from(context)
    notificationManager.notify(notificationId, createNotification(context, activity, layout, channelId))
}

4. 优化RemoteViews的上下文参数

将activity.packageName替换为context.packageName,避免Activity上下文可能引发的异常:

RemoteViews(context.packageName, layout)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 03:24:21