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):
- Screen is turned off
- Something happens that triggers a notification
- Screen is turned on *action for notification is performed*
- 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