Wednesday, December 11, 2013

Shouldn't this be a common notification pattern?

When an activity or background task creates a notification on Android, it specifies the Intent which usually refers to an activity that should be opened when the notification is clicked. However, it is surprising that the association between notification and activity is only in one direction. It seems to me that it would be a common pattern for an activity to need to clear notifications that open the activity because the notification may be moot once the activity is shown to the user. Additionally, while that activity is in the foreground, asynchronous background tasks should be able to be prevented from showing any new notification during the time that the activity has the foreground.

To handle this problem, an application of the mediator pattern is needed where the mediator intercepts the creation of the notifications and tracks the foreground state of the activity (or even activity fragment). The mediator may need to be a long-lived service to maintain state and serve all background tasks of any lifespan.