NotificationCompat.Builder 中的 setTimeoutAfter 不起作用
setTimeoutAfter in NotificationCompat.Builder doesn't work
我在文档中看到这个方法:
public NotificationCompat.Builder setTimeoutAfter(long durationMs)
指定应取消此通知的时间(如果尚未取消)。
文档没有提到兼容性,但是,当我尝试在低于 API 26 的设备上使用此方法时,它不起作用。我的代码:
// Init channel with default importance if api >= 26
initNotificationsChannel()
val notificationManager = NotificationManagerCompat.from(this)
val notification = NotificationCompat.Builder(this, channelId)
.setContentTitle("Title")
.setContentText("Text")
.setSmallIcon(R.drawable.icon)
.setTimeoutAfter(5000)
.build()
notificationManager.notify(1, notification)
这种行为是错误吗?
setTimeoutAfter
仅在 API 级别 26 中添加。
在旧平台上它将被忽略(当在 NotificationCompat.Builder
中使用时)。
我最终使用了以下解决方法:
notificationManagerCompat.notify(ID, myNotification);
new Handler(Looper.getMainLooper()).postDelayed (() -> {
notificationManagerCompat.cancel(ID);
}, DELAY);
我在文档中看到这个方法:
public NotificationCompat.Builder setTimeoutAfter(long durationMs)
指定应取消此通知的时间(如果尚未取消)。
文档没有提到兼容性,但是,当我尝试在低于 API 26 的设备上使用此方法时,它不起作用。我的代码:
// Init channel with default importance if api >= 26
initNotificationsChannel()
val notificationManager = NotificationManagerCompat.from(this)
val notification = NotificationCompat.Builder(this, channelId)
.setContentTitle("Title")
.setContentText("Text")
.setSmallIcon(R.drawable.icon)
.setTimeoutAfter(5000)
.build()
notificationManager.notify(1, notification)
这种行为是错误吗?
setTimeoutAfter
仅在 API 级别 26 中添加。
在旧平台上它将被忽略(当在 NotificationCompat.Builder
中使用时)。
我最终使用了以下解决方法:
notificationManagerCompat.notify(ID, myNotification);
new Handler(Looper.getMainLooper()).postDelayed (() -> {
notificationManagerCompat.cancel(ID);
}, DELAY);