4

I am showing a notification with RemoteInput like this:

   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());

output:

notification

after clicking on add note, entering text and submitting it I try to cancel notification like this:

notificationManager.cancel(123456);

which does not cancel notification but just dismiss the input field with text appended below my notification like this:

enter image description here

why does this not cancel the notification? and how to cancel it.

update: same results even there is a tag with notification

Rahul Tiwari
  • 5,931
  • 2
  • 41
  • 66

3 Answers3

1

After a while, I found a workaround, definitely not the most elegant solution. Issue happens for me on Android 9, whereas the notification with remote input is made undismissable by the system. The workaround is, after the user enters the text and clicks, and we need to update the notification UI, to use setTimeoutAfter(); even with values as low as 1 millisecond, the notification is removed after few seconds, so the solution is not the best.

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)
}
adiga
  • 28,937
  • 7
  • 45
  • 66
Alessio
  • 2,696
  • 13
  • 16
0

Try setting a tag to your notification and then provide that tag while you perform cancel as following:

While creating(Replace my_tag with your preferred unique tag):

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

While canceling:

notificationManager.cancel("my_tag",123456);
Mayur Gajra
  • 2,009
  • 2
  • 8
  • 18
0

When I create a notification with an opportunity to answer inside of it (as in the example with RemoteInput) and want to do NOT REPLACE it with other notification after the answer:

notificationManager.notify(tag, requestCode, notification);

but just to CANCEL it right after the answer with

notificationManager.cancel(tag, requestCode);

it does not disappear from the notification panel.

More over when I try to find out a list of existing notifications AFTER above cancel with

StatusBarNotification[] barNotifications = notificationManager.getActiveNotifications();

this (still not disappeared from the notification panel) notification IS ABSENT int the list!!

This bug appears starting from API 29 only and does not depend am I answering from the notification panel or from the "heads-up" notification.

As a workaround I have to replace an answered notification as in the manual with RemoteInput)

notificationManager.notify(tag, requestCode, notification);

wait a couple seconds

Thread.sleep(2000) 

and cancel it after

notificationManager.cancel(tag, requestCode);
isabsent
  • 3,219
  • 3
  • 21
  • 44