Android: Make Your Notification Sticky

Posted by in Software

Following my quest from the previous article, I realised that my app would not be in the foreground, like an email, browser or a game might be, but it would work without any GUI, using something like a cron job. This is all nice and lovely, but I’m not fond of applications doing stuff behind my back.

I’ve seen in various applications that do something similar a pattern: there’s something on the screen telling me (the user) the application is running. That “something” is usually an icon on the status bar, coming from a notification. Moreover, that notification is always present.

Normal notifications disappear after you swipe them away (aka “OK, I’ve seen you”) or touch the notification (aka “Give me more details”). The notifications I’m thinking of are persistent; they don’t go away if clicked. They are “sticky”.

Android makes it easy to make sticky notifications; you just need to add Notification.FLAG_NO_CLEAR to the set of notification flags. I have found that you need also Notification.FLAG_ONGOING_EVENT. Otherwise the notification may disappear after a while.

The makeNotification() method from the previous post becomes:

Simple.

HTH,

PS: Have a look at the previous post for the reference implementation!


A little experiment: If you find this post and ad below useful, please check the ad out :-)