Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: make notification persist across phone reboot

What's the best way to have a status bar notification persist when the phone is turned off and on again? The only solution I can think of is to create the notification in a Service which starts in response to the BOOT_COMPLETED_ACTION Intent.

like image 670
Graham Borland Avatar asked Jan 20 '23 10:01

Graham Borland


1 Answers

The only solution I can think of is to create the notification in a Service which starts in response to the BOOT_COMPLETED_ACTION Intent.

For raising a Notification, you can probably get by with just doing it in the BOOT_COMPLETED_ACTION BroadcastReceiver, rather than delegating it to a service. However, I agree, this is the only way to do it AFAIK.

Just be sure you do not irritate your users by doing this. Most people expect a relatively clean slate when they reboot their phone. Android assumes that notifications are no longer relevant with a reboot, which is why they do not persist.

So, for example, suppose you were writing an email client, and you use notifications to let the user know about unread messages. The answer should not be "redisplay the unread-message notification after a reboot". The answer should be "check for unread messages after a reboot, and raise the notification if there are unread messages". This way, if there are no unread messages (e.g., user had the phone off for a while and took care of their email on their PC or tablet), they do not get a spurious notification.

like image 100
CommonsWare Avatar answered Jan 28 '23 18:01

CommonsWare