gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:56:11 28 4
gpt4 key购买 nike

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

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com