I want to add my app to the notification bar so that it always shows, like some apps in the Google Play store.
I want it to be like this screen shot:
In order to have your notification always present, you'll want to set these two flags:
notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
Note that while setting your Service to be in the foreground will also get you an ongoing event, that is a very inappropriate thing to do unless you truly do need your Service to run in the foreground. A music player is a good example of an app that should do that -- the user has an expectation that their music will play without interruption, even when doing many other things with the device.
Most Services, however, can afford to be temporarily stopped by the system when memory is low, and then restarted automatically when memory is available again. So the correct way to think about it is to separate the two ideas.
Service.startForeground()
, but don't think of this as a way to get an ongoing notification.If you want your application to be present on the status bar at all times, you have to write a service and call startForeground(id, notification)
in the onStart(...)
and onStartCommand(...)
methods and respectively call the stopForeground()
method in the onDestroy()
method of the service.
The id is an integer you can assign to the notification and notification is a Notification
object (you can read about it more here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html).
This way as long as your service is running, the status bar notification will show.
Notification notification = new Notification(R.drawable.statusbar_icon,
"Rolling text on statusbar", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, YourActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this,
"Notification title", "Notification description", contentIntent);
startForeground(1, notification);
You can put this code in the service's onStart(...)
and onStartCommand(...)
methods.
Also you can read more on services here: http://developer.android.com/reference/android/app/Service.html
Just use below code to always show notification bar.
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("Call Recorder")
.setAutoCancel(false);
Notification notification = builder.getNotification();
notification.flags |= Notification.FLAG_NO_CLEAR
| Notification.FLAG_ONGOING_EVENT;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
Here's example using NotificationCompact.Builder class which is the recent version to build notification.
private void startNotification() {
//Sets an ID for the notification
int mNotificationId = 001;
// Build Notification , setOngoing keeps the notification always in status bar
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ldb)
.setContentTitle("Stop LDB")
.setContentText("Click to stop LDB")
.setOngoing(true);
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Build the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}