Android Notification Bar

Android Notification Bar

A notification is a message you can display to the user outside of your application’s normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.

Notifications are used to alert users on some events that requires their attention. Notifications alert the users through various forms:
  • Display a status bar icon
  • Flash the LED
  • Vibrate
  • Ringtones

Status bar notifications indicates some ongoing background services such as downloading or playing music or displays an alert/information.To see the notification user needs to open the notification drawer. Notifications are handled by Notification Manger in Android. Notification Manager is a system service used to manage notifications.

To create a status bar notification we need to use two classes, NotificationManager, Notification.

The NotificationManager is a system Service used to manage Notification. Get a reference to it using the getSystemService() method. The syntax is given below –

Using the Notification Manager you can trigger new notifications, modify existing ones, or cancel that are no longer required.

Create and Send Notifications

You have simple way to create a notification. Follow the following steps in your application to create a notification −

1 – Create Notification Builder

As a first step is to create a notification builder using You will use Notification Builder to set various Notification properties like its small and large icons, title, priority etc.

2 – Setting Notification Properties

Once you have Builder object, you can set its Notification properties using Builder object as per your requirement. But this is mandatory to set at least following −

  • A small icon, set by setSmallIcon()
  • A title, set by setContentTitle()
  • Detail text, set by setContentText()

3 – Attach Actions

This is an optional part and required if you want to attach an action with the notification. An action allows users to go directly from the notification to an Activity in your application, where they can look at one or more events or do further work.

The action is defined by a PendingIntent containing an Intent that starts an Activity in your application. To associate the PendingIntent with a gesture, call the appropriate method ofNotificationCompat.Builder. For example, if you want to start Activity when the user clicks the notification text in the notification drawer, you add the PendingIntent by calling setContentIntent().

A PendingIntent object helps you to perform an action on your applications behalf, often at a later time, without caring of whether or not your application is running.

We take help of stack builder object which will contain an artificial back stack for the started Activity. This ensures that navigating backward from the Activity leads out of your application to the Home screen.

4 – Issue the notification

Finally, you pass the Notification object to the system by calling NotificationManager.notify() to send your notification. Make sure you call method on builder object before notifying it. This method combines all of the options that have been set and return a new Notification object.

The NotificationCompat.Builder Class

The NotificationCompat.Builder class allows easier control over all the flags, as well as help constructing the typical notification layouts. Following are few important and most frequently used methods available as a part of NotificationCompat.Builder class.

Notification build()Combine all of the options that have been set and return a new Notification object.
NotificationCompat.Builder setAutoCancel (boolean autoCancel)Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel.
NotificationCompat.Builder setContent (RemoteViews views)Supply a custom RemoteViews to use instead of the standard one.
NotificationCompat.Builder setContentInfo (CharSequence info)Set the large text at the right-hand side of the notification.
NotificationCompat.Builder setContentIntent (PendingIntent intent)Supply a PendingIntent to send when the notification is clicked.
NotificationCompat.Builder setContentText (CharSequence text)Set the text (second row) of the notification, in a standard notification.
NotificationCompat.Builder setContentTitle (CharSequence title)Set the text (first row) of the notification, in a standard notification.
NotificationCompat.Builder setDefaults (int defaults)Set the default notification options that will be used.
NotificationCompat.Builder setLargeIcon (Bitmap icon)Set the large icon that is shown in the ticker and notification.
NotificationCompat.Builder setNumber (int number)Set the large number at the right-hand side of the notification.
NotificationCompat.Builder setOngoing (boolean ongoing)Set whether this is an ongoing notification.
NotificationCompat.Builder setSmallIcon (int icon)Set the small icon to use in the notification layouts.
NotificationCompat.Builder setStyle (NotificationCompat.Style style)Add a rich notification style to be applied at build time.
NotificationCompat.Builder setTicker (CharSequence tickerText)Set the text that is displayed in the status bar when the notification first arrives.
NotificationCompat.Builder setVibrate (long[] pattern)Set the vibration pattern to use.
NotificationCompat.Builder setWhen (long when)Set the time that the event occurred. Notifications in the panel are sorted by this time.


In this tutorial, we are going to learn how to create  Single Line Notification and Multi Line Notification in Notification Bar.

Video Demo

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is

2. Open res ⇒ values ⇒ strings.xml and add below string values. This are some strings that i am going to use in this tutorial.

3. Open your AndroidManifest.xml and add the following permission Vibrate permission to make vibrate device when notification occurs .

4. Now create a xml layout naming activity_main.xml and add the following views. In this layout i had taken some buttons for both single and multi line notification separately.

5. After creating activity_main.xml create a new xml layout naming notification_activity.xml for pending intent to show notification data in new activity.

6. Now, for creating Multi line notification we have to add the inbox style of notification. For this NotificationCompat.InboxStyle help us to achieve this. In this i had added 6 lines  and finally add the inboxstyle to notification builder.

7. Open your and add the following code. I am using two different NotificationId for both Single and MultiLine notification. We will use this ids to start notification, update notification and stop notification. I am also setting a long[] pattern for vibration and also alarm sound for notification.

When user clicks over notification a new activity will open and display  a message with current notificationId for this i had put intent values to next intent.

8. Finally create a new activity naming this activity open when user clicks over notification. In this activity i am getting notificationId and message and displaying it over textview.

9. Now, you are all done, run your app and you will get the output as shown in video.

Thanks. :)

Download Source Code


About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)