0

我正在尝试使用 Kotlin 语言在我的项目中实现自定义通知。但是,它不会显示在通知面板中,尽管它的默认设置是在按钮单击上工作。假设我删除了 ContentTitle 和 ContentText 并设置了自定义内容,那么只有通知响起,没有任何显示。

功能:

@RequiresApi(Build.VERSION_CODES.O)
@SuppressLint("RemoteViewLayout")
private fun showNotification() {


    val intent = Intent(this, MainActivity::class.java)
    val pendingIntent = TaskStackBuilder.create(this).run {
        addNextIntentWithParentStack(intent)
        getPendingIntent(0, PendingIntent.FLAG_IMMUTABLE)
    }

    val notificationManager =
        this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val channel = NotificationChannel(
        CHANNEL_ID,
        CHANNEL_NAME,
        NotificationManager.IMPORTANCE_HIGH
    )

    val contentView = RemoteViews(packageName, R.layout.notification_small)
    contentView.setTextViewText(R.id.tvHead, "Woohlah")
    contentView.setTextViewText(R.id.tvDes, "Working Very well")

    val builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notify_status)
        .setContentTitle("Yahoo")
        .setContentText("Koi Mujhe Junglee Kahe")
        .setStyle(NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(contentView)
        .setCustomBigContentView(contentView)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)

    notificationManager.createNotificationChannel(channel)
    notificationManager.notify(NOTIFICATION_ID, builder.build())
}

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:id="@+id/imageViewNotification"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_centerVertical="true"
    android:contentDescription="@string/dot"
    android:padding="20dp"
    android:src="@drawable/ic_notify_status" />

<TextView
    android:id="@+id/tvHead"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="10dp"
    android:layout_toEndOf="@+id/imageViewNotification"
    android:paddingTop="10dp"
    android:textColor="@color/white"
    android:textSize="18sp"
    android:textStyle="bold"
    tools:text="xxx" />

<TextView
    android:id="@+id/tvDes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvHead"
    android:layout_marginHorizontal="10dp"
    android:layout_toEndOf="@+id/imageViewNotification"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:textColor="@color/white"
    android:textSize="14sp"
    tools:text="xxx" />

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tvCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:padding="10dp"
    android:textColor="@color/black"
    android:textSize="14sp"
    tools:text="0" />
</RelativeLayout>
4

1 回答 1

0

根据文档,并非所有布局和小部件都受 RemoteViews 支持。你AppCompatTextView是造成问题的原因。将其替换为简单的TextView.

还,

请注意,通知的背景颜色可能因不同的设备和版本而异。因此,您应该始终应用支持库样式,例如 TextAppearance_Compat_Notification 用于文本,TextAppearance_Compat_Notification_Title 用于自定义布局中的标题。这些样式会适应颜色变化,因此您最终不会得到黑底黑字或白底白字。

文档:

于 2022-02-04T07:24:57.473 回答