notifications - setOngoing(true) 在 Oreo+ 通知中不起作用
问题描述
我正在使用 channel_id 创建一个自定义通知并将其作为前台启动。我正在播放背景音乐。 我的问题是当用户滑动通知时它消失了。我也有 setOngoing(true)。我希望在播放音乐时保留通知。
注意:它只发生在 Oreo+ 中。在较低版本中它工作正常。
public void CustomNotification() {
remoteViews = new RemoteViews(getPackageName(), R.layout.player_noti_layout);
notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default",
getString(R.string.player_channel),
NotificationManager.IMPORTANCE_LOW);
channel.enableVibration(true);
channel.enableLights(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.setDescription("Notification, Play/pause & Next/Prev");
notificationmanager.createNotificationChannel(channel);
}
builder = new NotificationCompat.Builder(this, "default");
Notification foregroundNote;
// Set icon
foregroundNote = builder.setSmallIcon(R.drawable.ic_radio)
.setLargeIcon(R.drawable.cool_1)
// Set ticker message
.setTicker(getResources().getString(R.string.app_name))
// Dismiss notification
.setAutoCancel(false)
.setOngoing(true)
.setContent(remoteViews)
.setContentTitle("title").
setContentText("text")
.build();
foregroundNote.flags |= Notification.FLAG_ONGOING_EVENT;
foregroundNote.flags |= Notification.FLAG_NO_CLEAR;
foregroundNote.contentView = remoteViews;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
startForeground(2, foregroundNote);
} else
notificationmanager.notify(2, foregroundNote);
}
解决方案
简而言之,这是由于碎片化和定制化造成的。可能该电话具有某种设置来配置该行为。正如有人有一天所说:有些手机只是-垃圾-。不要浪费时间尝试为每部可能的手机解决问题。
推荐阅读
- swift - 如何解决 Swift 协议的规范问题
- javascript - 禁用 react-native 中的黄色警告消息
- python-3.x - 如何从给定的句子中提取更长的字符串并忽略子字符串?
- python - 一个python程序,它掷两个骰子并计算尝试次数,直到这些骰子的数量都是6
- python - 如何用 matplotlib 绘制一个 numpy 数组?
- ios - 我如何知道用户在商店中有应用内购买项目?
- ios - 为什么 Distriqt ANE for iOS 中的折扣促销优惠在生产模式下不起作用?
- python - 如何将 hdf 中的网格化卫星条带数据转换为 NASA GPM 卫星提供的 netcdf4?
- wso2 - WSO2APIM 网关副本同步延迟
- vue.js - 将 Vue App 部署到 FTP 服务器需要哪些文件?