0

I have an Android app with a working feature which I have now migrated to Android 9 (previously targeting Android 8) which somewhat broke the feature.

I have this flow (when app is started):

  1. Screen is turned off
  2. Something happens that triggers a notification
  3. Screen is turned on *action for notification is performed*
  4. Notification disappears from notification bar automatically

After targetting Android 9 the notification does not disappear in step 4. The consequence is that after I've performed the action I can still tap the notification and the action happens yet again (unwanted behavior).

This change of behavior seems to be undocumented (previous default was to dismiss the notification after the action is performed). How can I get my app to work the old way?

Question: How do I get the old behavior back? (notification dismissed automatically when action is triggered and the user has seen it)

Update 1: Some (pruned/simplified) code to hopefully clarify how this works

public class FooActivity extends AppCompatActivity {
}

public class NotificationUtil {
   // A lot of boilerplate omitted where notification channels are created
   public void notify(Intent startActivity) {  
       ContextProvider.getSystemService(Context.NOTIFICATION_SERVICE).notify("NotificationUtil", 0,  new NotificationCompat.Builder().nextIntent(startActivity).x().y().build());
   }
}

So step 2 triggers NotificationUtil.notify() with an Intent action set to FooActivity. FooActivity is launched as expected automatically when turning on the screen (e.g. the notification is "consumed" implicitly in my opinion. But the notification remains in the notification bar and it's possible to tap and launch FooActivity again (unwanted behavior).

Update 2: The question is not how to programmatically dismiss the notification (this is answered by How to dismiss notification after action has been clicked) but rather when to do it so that the notification is not dimissed until when the user actually looks at it.

References

  1. https://developer.android.com/about/versions/pie/android-9.0-changes-28
  2. https://developer.android.com/guide/topics/ui/notifiers/notifications
Alix
  • 2,630
  • 30
  • 72
  • It would be helpful to add some example code to clarify what your current solution is doing – trker May 24 '19 at 13:36
  • Sure, I'll update the post with more details – Alix May 24 '19 at 13:42
  • To whomever voted for the post to be closed, what is still missing in my question? – Alix May 24 '19 at 14:05
  • 1
    Possible duplicate of [How to dismiss notification after action has been clicked](https://stackoverflow.com/questions/11883534/how-to-dismiss-notification-after-action-has-been-clicked) – MSpeed May 24 '19 at 14:13
  • @Speed It's on the same topic but different questions I would say. This is much more specific – Alix May 24 '19 at 14:17
  • Do these help? [Keep track of the notification ID and manually call cancel in the oncreate method](https://stackoverflow.com/a/32892342/6860684) or [setAutoCancel(true) in the notification builder](https://stackoverflow.com/a/16845530/6860684) ? – trker May 24 '19 at 14:51
  • @trker autoCancel(true) is set already. Manually calling cancel would work but when? If in onCreate() the notification is cancelled immediately (before user turns on screen) – Alix May 24 '19 at 14:54
  • 1
    `setAutoCancel` only applies to the content intent, not to actions. – ianhanniballake May 24 '19 at 16:46
  • Stuff that I've tried (which does not work) - Cancel notifications in "onResume()" and "onWindowAttached()" for the activity that I want to launch. The notification is cancelled too soon. I want the notification to run until the user actually turns on the screen and sees the activity – Alix May 27 '19 at 11:36
  • Now you have @ianhanniballake looking into it... and if I'm not mistaken, Ian works @ Google :) – Martin Marconcini Jun 03 '19 at 14:27

0 Answers0