android 通知面板中未显示自定义通知
Custom notification is not showing in the android notification panel
我正在尝试使用 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>
根据文档,RemoteView 并非支持所有布局和小部件。您的 AppCompatTextView
导致了问题。将其替换为简单的 TextView
.
此外,
Beware that the background color for the notification can vary across different devices and versions. So you should always apply support library styles such as TextAppearance_Compat_Notification for the text and TextAppearance_Compat_Notification_Title for the title in your custom layout. These styles adapt to the color variations so you don't end up with black-on-black or white-on-white text.
文档:
我正在尝试使用 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>
根据文档,RemoteView 并非支持所有布局和小部件。您的 AppCompatTextView
导致了问题。将其替换为简单的 TextView
.
此外,
Beware that the background color for the notification can vary across different devices and versions. So you should always apply support library styles such as TextAppearance_Compat_Notification for the text and TextAppearance_Compat_Notification_Title for the title in your custom layout. These styles adapt to the color variations so you don't end up with black-on-black or white-on-white text.
文档: