5

我正在显示这样的通知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());

输出:

通知

单击添加注释,输入文本并提交后,我尝试取消通知,如下所示:

notificationManager.cancel(123456);

它不会取消通知,而只是关闭输入字段,并在我的通知下方附加文本,如下所示:

在此处输入图像描述

为什么这不取消通知?以及如何取消它。

更新:即使有带有通知的标签,结果也相同

4

3 回答 3

3

过了一会儿,我找到了一个解决方法,绝对不是最优雅的解决方案。我在 Android 9 上发生了问题,而系统无法关闭带有远程输入的通知。解决方法是,用户输入文字并点击后,我们需要更新通知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)
}
于 2019-07-03T10:02:02.397 回答
1

当我创建一个有机会在其中回答的通知(如示例中的RemoteInput)并且不想在回答后用其他通知替换它时:

notificationManager.notify(tag, requestCode, notification);

但只是在回答后立即取消它

notificationManager.cancel(tag, requestCode);

它不会从通知面板中消失。

更重要的是,当我尝试在上述取消之后找出现有通知的列表时

StatusBarNotification[] barNotifications = notificationManager.getActiveNotifications();

这个(仍然没有从通知面板中消失)通知列表中不存在!

错误仅从 API 29 开始出现,并不取决于我是从通知面板还是从“提醒”通知中回答的。

作为一种解决方法,我必须将手册中的已回答通知替换为RemoteInput)

notificationManager.notify(tag, requestCode, notification);

等几秒钟

Thread.sleep(2000) 

并在之后取消

notificationManager.cancel(tag, requestCode);
于 2020-06-17T09:18:19.910 回答
0

尝试为您的通知设置标签,然后在执行取消时提供该标签,如下所示:

创建时(将 my_tag 替换为您喜欢的唯一标签):

notificationManager.notify("my_tag",123456, builder.build());

取消时:

notificationManager.cancel("my_tag",123456);
于 2019-01-16T15:55:14.793 回答