自定义通知不起作用-为什么这样?

时间:2019-01-30 16:26:19

标签: android android-notifications

尽管默认通知在我的应用上正常运行,但自定义通知由于某些原因无法正常工作

代码->

val notificationLayout = RemoteViews(packageName, R.layout.layout_new_message_notification)

val builder = NotificationCompat.Builder(this@HomeActivity, "DEFAULT")
                .setSmallIcon(R.drawable.logo_silhoutte)
                .setColor(ContextCompat.getColor(this@HomeActivity, R.color.colorAccent))
                .setCustomContentView(notificationLayout)
                .setStyle(NotificationCompat.DecoratedCustomViewStyle())

val not = builder.build();
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            val channel = NotificationChannel("DEFAULT", "DEFAULT", NotificationManager.IMPORTANCE_HIGH)
            notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0, not)

删除这两行->

                .setCustomContentView(notificationLayout)
                .setStyle(NotificationCompat.DecoratedCustomViewStyle())

布局文件(layout_new_message_notification.xml)->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">

<com.siddhantkushwaha.raven.custom.SquareImageView
    android:id="@+id/displayPic"
    android:layout_width="48dp"
    android:layout_height="wrap_content"
    android:src="@drawable/image_unknown_user_circle" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:text="Notification Title"
        android:textColor="@color/colorBlack"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:text="This is a message."
        android:textColor="@color/colorBlack"
        android:textSize="16sp" />

</LinearLayout>

1 个答案:

答案 0 :(得分:-1)

要自定义通知,可以使用以下命令: 1.在Android N之前

builder.setContent(remoteViews) builder.setCustomBigContentView(bigRemoteViews)

2.Android N及以上 setCustomContentView() setCustomBigContentView()

对于样式,可以使用“ DecoratedMediaCustomViewStyle()”