我使用RemoteInput
显示如下通知:
RemoteInput remoteInput = new RemoteInput.Builder("key_add_note")
.setLabel("add note")
.build();
PendingIntent AddNotePendingIntent =
PendingIntent.getBroadcast(getApplicationContext(),
(int) txn.get_id(),
new Intent(getApplicationContext(), AddNoteBroadcastReceiver.class)
.putExtra(Constants.IntentExtras.STA_TXN_ID, txn.get_id()),
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.ic_action_edit_dark,
"add note", AddNotePendingIntent)
.addRemoteInput(remoteInput)
.build();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationUtil.MISC_CHANNEL_ID)
.setContentTitle("TEST")
.setContentText("add Note")
.setSmallIcon(R.drawable.ic_action_edit_dark)
.setAutoCancel(true)
.addAction(action);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(123456, builder.build());
输出:
点击add note,输入文本并提交后,我尝试取消通知,如下所示:
notificationManager.cancel(123456);
它不会取消通知,而只是关闭在通知下面附加了文本的输入字段,如下所示:
为什么不取消通知呢?以及如何取消它。
更新:即使有带通知的标签,结果也是一样的
发布于 2019-07-03 10:02:02
过了一段时间,我发现了一个变通的办法,绝对不是最优雅的解决方案。问题发生在我的Android9上,而远程输入的通知则被系统设置为不可忽略。解决方法是,在用户输入文本并单击之后,我们需要使用update the notification UI,才能使用setTimeoutAfter()
;即使值低至1毫秒,通知也会在几秒钟后删除,因此解决方案并不是最好的。
fun updateNotification(context: Context, id: Int) {
val notification = NotificationCompat.Builder(context, MY_CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_action_edit_dark)
.setContentText(MY_TEXT)
.setTimeoutAfter(1)
.build()
// show notification. This hides direct reply UI
NotificationManagerCompat.from(context).notify(id, notification)
}
发布于 2020-06-17 09:18:20
当我创建了一个有机会在其中应答的通知(就像在带有RemoteInput
的example中一样),并且想要在应答之后用其他通知替换它,而不是用其他通知替换时:
notificationManager.notify(tag, requestCode, notification);
但是要在答案后立即取消,请使用 it
notificationManager.cancel(tag, requestCode);
它不会从通知面板中消失。
更重要的是,当我尝试找出一个现有的通知列表后,上述取消
StatusBarNotification[] barNotifications = notificationManager.getActiveNotifications();
此(仍未从通知面板中消失)通知不存在于列表中!!
此错误仅从API29开始出现,并不依赖于我是从通知面板还是从“提醒”通知回答。
作为一种解决方法,我必须用RemoteInput
替换manual中已应答的通知)
notificationManager.notify(tag, requestCode, notification);
等几秒钟
Thread.sleep(2000)
然后取消它
notificationManager.cancel(tag, requestCode);
发布于 2019-01-16 15:55:15
尝试为通知设置标记,然后在执行取消操作时提供该标记,如下所示:
创建时(将my_tag替换为您首选的唯一标记):
notificationManager.notify("my_tag",123456, builder.build());
取消时:
notificationManager.cancel("my_tag",123456);
https://stackoverflow.com/questions/54219914
复制