Android: How to create an "Ongoing" notification?
AndroidNotificationsAndroid Problem Overview
Hello, How do i create the permanent notification like the first one for Battery Indicator?
Android Solutions
Solution 1 - Android
In case you are using NotificationCompat.Builder
, you can use :
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
mBuilder.setOngoing(true); //this will make ongoing notification
Solution 2 - Android
Assign Notification.FLAG_ONGOING_EVENT
flag to your Notification
.
Sample code:
yourNotification.flags = Notification.FLAG_ONGOING_EVENT;
// Notify...
If you aren't familiar with the Notification
API, read Creating Status Bar Notifications on Android developers website.
Solution 3 - Android
It is actually recommended to bitwise-or notification flags, rather than set the flags directly. This allows you to set multiple flags at once.
For example:
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
will set both flags at once, whereas:
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
will only set the FLAG_SHOW_LIGHTS flag.
Solution 4 - Android
public static final int FLAG_ONGOING_EVENT
Since: API Level 1
Bit to be bitwise-ored into the flags field that should be set if this notification is in reference to something that is ongoing, like a phone call.
public static final int FLAG_FOREGROUND_SERVICE
Since: API Level 5 Bit to be bitwise-ored into the flags field that should be set if this notification represents a currently running service.