java - 当我使用 .setStyle 时,通知未显示在 android Oreo 中
问题描述
在 android Oreo 中创建通知时,一切正常,直到我使用 .setStyle 然后它停止工作
代码:
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_HIGH;
{
NotificationChannel mChannel = new NotificationChannel(
CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
assert notificationManager != null;
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle(track.getTitle())
.setContentText(track.getArtist())
.setSmallIcon(R.drawable.icon)
.setOnlyAlertOnce(true)//show notification for only first time
.setLights(Color.CYAN, 500, 1200) //Setting the color of the led blink on the phone for notifications
.addAction(drw_previous, "Previous", pendingIntentPrevious) //Setting action for the previous button
.addAction(playbutton, "Play", pendingIntentPlay) //Setting the action for the play/pause button
.addAction(drw_next, "Next", pendingIntentNext) //Setting the action for he next button
.setDeleteIntent(pendingIntentDelete) //Setting delete intent for when the notification is destroyed/deleted
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setMediaSession(mediaSessionCompat.getSessionToken())) //Setting how and what order the buttons will be shown
.setShowWhen(false) //Do not show time for when this notification was created
.setDefaults(Notification.DEFAULT_ALL); //Defaults
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
我是不是错过了什么。我拥有一切应该让它发挥作用的东西。它曾经可以工作,但我正在重新审视代码并对其进行更改并使其变得更好,并且不确定这次出了什么问题。感谢您的帮助!
解决方案
推荐阅读
- reactjs - 如何使用带有情感样式的 MUI v5 使用条件样式
- python - Python USACO 青铜级算法题:逻辑在理论上是合理的,但不知道如何实现
- c# - 除了它所针对的框架之外,其他框架是否支持 C# 语言版本?
- javascript - 如何在 LimberGridView 的左下角获取调整大小助手?
- c# - Rx.NET 中是否存在功能类似于 BehaviorSubject 但仅在值发生更改时才发出的 Subject 实现?
- xcode - Xcode 在禁用的代码行上触发断点
- flutter - 永远不会调用 ChangeNotifierProxyProvider 创建函数
- angular - 打字稿将计算值添加到类
- javascript - 如何使用 Javascript 自动滚动 ReactJS 项目类
- java - 如何将两个不同集合中的两个字段相乘