【问题标题】:cancel notification with remoteInput not working使用 remoteInput 取消通知不起作用
【发布时间】:2019-01-16 15:04:49
【问题描述】:

我正在显示带有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);

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

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

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

【问题讨论】:

  • 如果这个特定的答案(类似问题)有效,请告诉我:stackoverflow.com/a/20007350/2710385
  • @dylan-myers 不,它没有。
  • 有同样的问题,也使用自定义 int 标签(不是 1)。你解决过这个问题吗?
  • 我在处理这个项目时无法解决它。

标签: android android-notifications remote-input


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    当我创建一个有机会在其中回答的通知时(如在exampleRemoteInput 中)并希望在回答后不替换它与其他通知:

    notificationManager.notify(tag, requestCode, notification);
    

    但只是在回答后立即取消

    notificationManager.cancel(tag, requestCode);
    

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

    当我尝试在上述取消之后找出现有通知的列表时,更多信息

    StatusBarNotification[] barNotifications = notificationManager.getActiveNotifications();
    

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

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

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

    notificationManager.notify(tag, requestCode, notification);
    

    等待几秒钟

    Thread.sleep(2000) 
    

    之后取消

    notificationManager.cancel(tag, requestCode);
    

    【讨论】:

      【解决方案3】:

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

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

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

      取消时:

      notificationManager.cancel("my_tag",123456);
      

      【讨论】:

      猜你喜欢
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多