Android: Make Your Notification Sticky

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:



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 :-)

12 thoughts on “Android: Make Your Notification Sticky

  1. Hey!

    Really helpful tutorial (this and the previous one). Works very well. However, I had a question. How do I ensure that the notification is back and running when device is rebooted? With this implementation, the notification is lost once you restart the device.

    Thanks again :)

  2. You can support down to android API v1 by using:

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)

    instead of:

    Notification.Builder builder = new Notification.Builder(context)

    and everything will work fine in all android versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top