Wednesday, November 21, 2012

Notifications In Android

What is Notification

Android allows to put notification into the titlebar of your application. The user can expand the notification bar and by selecting the notification the user can trigger another activity.

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.

Elements of Notification

  1. Content title
  2. Large icon
  3. Content text
  4. Content info
  5. Small icon
  6. Time that the notification was issued.

Creating a Notification

We need to specify the UI information and actions for a notification in a NotificationCompat.Builder object. To create the notification itself, you call, which returns a Notification object containing your specifications. To issue the notification, you pass the Notification object to the system by calling NotificationManager.notify().

A Notification object must contain the following:

  • A small icon, set by setSmallIcon()
  • A title, set by setContentTitle()
  • Detail text, set by setContentText()
The following code snippet illustrates a simple notification that specifies an activity to open when the user clicks the notification. Notice that the code creates a TaskStackBuilder object and uses it to create the PendingIntent for the action

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

// The stack builder object 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.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
// Adds the Intent that starts the Activity to the top of the stack
PendingIntent resultPendingIntent =
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.

Removing or Cancelling Notifications

Notifications remain visible until one of the following happens:

The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared).
The user clicks the notification, and you called setAutoCancel() when you created the notification.
You call cancel() for a specific notification ID. This method also deletes ongoing notifications.
You call cancelAll(), which removes all of the notifications you previously issued.


  1. Nice post with great details. I really like your job. Thanks for sharing. mass notification software

  2. Hi,

    I need your help for Notification....My app is dealing with sensex related data. I want to setAlarms for specific sensex value whether my app is running or closed. e.g if I set Alarm in my app for gold rates of 25000, whenever sensex value reach to 25000 i should get alarm or notification whether my app is running or not....It's very urget plz!!!!

  3. Hi,

    I want your help for sending of Notifications. In my app i have to set notifications from 5 activities, how to set notifications from all activities at user selected time and repeated days like(mon,tue,fri,... and every hour, every two hour, every six hour, etc,..)

  4. thanks you for sharing this tutorial. I am a newbie and i find tutorials on your site very helpful. I am a big fan of learning by doing. Another important trick is to download an apk from apkmonk or apkmirror then decompile it using java compiler. Using this technique one can get code written in production environments.

  5. Thanks so much for this information. sb game hacker apk I have to let you know I concur on several of the points you make here and others may require some further review, game killer apk but I can see your viewpoint. geometry dash full apk

  6. thanks. Actually i like notification bar on Android 4.4 more than Android 5.x

  7. useful information for learner like me. Thank you. I also recommend the app game :brain it on for pc . read guideline here

  8. ​Thank for your great post.

    I have been able to create my first Android app after reading, and in addition, I would like to suggest everybody to check out android quiz and interview questions, it may be helpful for one who is preparing for a job interview

  9. Indus Valley Mukteshwar constructed & delivered several deluxe cottages & apartments in Mukteshwar, Nainital. Contact for more information.

    cottages in nainital for sale

  10. This comment has been removed by the author.

  11. This comment has been removed by the author.

  12. uktvnow apk : in the event that you need to watch online most adored motion pictures and serials. click on uktvnow and watch online most adored motion pictures and tv now apk is an well known app for movies.go and uktvnow download.

  13. Hai i liked your site and grt most of the important information from here and liked that page i wants to show some apps which are blocked from you tube and you can download from here Show Box APK

  14. I really liked and enjoy your site. I have tell you about the Spotify apk. This is a app which is most famouse music app.
    Spotify for pcs/laptops
    Spotify free apk

  15. Wonderful blog, thanks for sharing this type of information. Your website gives the best idea about my interesting information. Any issue related to Online acting classes you can visit the ICE-Balaji website.

  16. First of all, thank you for your post. Your posts are neatly organized with the information I want, so there are plenty of resources to reference. I bookmark this site and will find your posts frequently in the future. Thanks again ^^์˜จ๋ผ์ธ์นด์ง€๋…ธ