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

标签 android notifications android-notifications

此代码有效,每次都会显示通知,但如果我取消注释行 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>

最佳答案

问题在于布局:

  • 根布局应为 RelativeLayout
  • 使用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>
    
    

    关于android - 为什么不显示带有 setCustomContentView 的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66176500/

    相关文章:

    android - 使用卫星与 Android 手机进行双向通信

    javascript - 显示 Chrome 扩展程序的通知

    android - 前台启动通知错误,服务通知 channel 无效 :

    java - 根据通知操作点击打开不同的 Activity

    android - 如何使用 Android 在后台发送短信?

    java - findFragmentById() 和在 Android 中创建 Fragment 对象有什么区别?

    android - 如何在我的 Android 应用程序中设置 DeepLink

    android - 如何在未调用 onDestroy() 时清除状态栏通知?

    android - 从最近的托盘中删除 Android 应用程序时,Firebase(FCM)通知不会出现

    android - 在收到 gcm 通知时显示多个通知