我正在尝试制作一个 android 播放器并正在为它制作一个通知。在virtual pixel2 API30上会显示通知,但在搭载Android 10的小米红米9C智能手机上却没有通知。
通知构建代码:
private fun showNotification() {
val remoteViews = RemoteViews(packageName, R.layout.notification_playing)
if (nowPlayingTrack != null) {
remoteViews.setTextViewText(R.id.notification_title, nowPlayingTrack!!.tName)
remoteViews.setTextViewText(R.id.notification_description, nowPlayingTrack!!.tAuthor)
remoteViews.setImageViewResource(R.id.notification_image, R.drawable.track)
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, Intent(), PendingIntent.FLAG_IMMUTABLE)
val notificationBuilder = NotificationCompat.Builder(this.applicationContext,
NOTIFICATION_CHANNEL_ID)
notificationBuilder
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.track_without_bg)
.setSilent(true)
.setContentTitle("Title")
.setContentText("text")
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCustomContentView(remoteViews)
val notificationManager =
this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationChannel = NotificationChannel(
"vladik_music",
NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
)
val notification = notificationBuilder.build()
notification.flags = Notification.FLAG_NO_CLEAR
notificationTarget = NotificationTarget(this, R.id.notification_image,
remoteViews, notification, id)
notificationManager.createNotificationChannel(notificationChannel)
with(NotificationManagerCompat.from(this)) {
this.notify(id, notification)
}
if (nowPlayingTrack!!.getPhoto() != null) {
Glide.with(this@VladikMusicPlayService)
.asBitmap()
.load(nowPlayingTrack!!.getPhoto()!!.medium)
.transform(RoundedCornersTransformation(50, 0))
.into(notificationTarget)
}
}
}
布局代码(适用于所有人)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/notification_main_layout"
android:padding="5dp">
<ImageView
android:layout_centerVertical="true"
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/notification_image"/>
<TextView
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginHorizontal="15dp"
android:layout_marginVertical="5dp"
android:textStyle="bold"
android:id="@+id/notification_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/notification_image"
android:maxLines="1"/>
<TextView
android:id="@+id/notification_description"
android:textSize="15sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/notification_image"
android:layout_below="@+id/notification_title"
android:layout_marginHorizontal="15dp"
android:maxLines="1" />
</RelativeLayout>
我试图更改 ID,删除自定义视图,但没有任何帮助。请帮忙