为什么不显示带有 setCustomContentView 的通知?

Why notification with setCustomContentView is not displaying?

此代码有效,每次都会显示通知,但如果我取消注释 setContent, setCustomContentView, setCustomBigContentView 行,则通知不会显示。为什么它不与 CustomContentView 一起显示?

var notificationId = 1

    private fun displayNotification() {
        createNotificationChannel()

        val notificationLayout = RemoteViews(context.packageName, R.layout.view_notification_collapsed)

        val notification = NotificationCompat.Builder(context, "CHANNEL_ID")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setAutoCancel(true)
            .setShowWhen(true)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setCategory(NotificationCompat.CATEGORY_SOCIAL)
            .setStyle(NotificationCompat.DecoratedCustomViewStyle())
            //.setContent(notificationLayout)
            //.setCustomContentView(notificationLayout)
            //.setCustomBigContentView(notificationLayout)
            .setContentIntent(
                PendingIntent.getActivity(
                    context,
                    0,
                    Intent(context, MainActivity::class.java),
                    0
                )
            )
            .build()

        val notificationManager = NotificationManagerCompat.from(context)
        notificationManager.notify(notificationId++, notification)
    }

    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return
        }
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val notificationChannel = notificationManager.getNotificationChannel("CHANNEL_ID")
        if (notificationChannel == null) {
            val channel = NotificationChannel(
                "CHANNEL_ID","channel_name",
                NotificationManager.IMPORTANCE_DEFAULT
            ).apply {
                description = getString(R.string.notification_channel_description)
            }

            notificationManager.createNotificationChannel(channel)
        }
    }

view_notification_collapsed.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    android:padding="8dp">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/content_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Notification Sample App"
        android:textAppearance="@style/TextAppearance.Compat.Notification.Title"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

布局有问题:

  1. 根布局应该是RelativeLayout
  2. 使用TextView代替androidx.appcompat.widget.AppCompatTextView

所以布局应该是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
    android:padding="8dp">

    <TextView
        android:id="@+id/content_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Notification Sample App"
        android:textAppearance="@style/TextAppearance.Compat.Notification.Title"
        />

</RelativeLayout>