Android: How to create an "Ongoing" notification?

AndroidNotifications

Android Problem Overview


enter image description here

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRohith NandakumarView Question on Stackoverflow
Solution 1 - AndroidMahmoud FarahatView Answer on Stackoverflow
Solution 2 - AndroidWroclaiView Answer on Stackoverflow
Solution 3 - AndroidjoebobfrankView Answer on Stackoverflow
Solution 4 - AndroidAleadamView Answer on Stackoverflow