Android Notifications

Last modified on August 1st, 2014 by Joe.

In this tutorial we shall learn about different types of notifications in android. Following are the three types of android notifications,

  1. Toast Notification – Shows message that fades away after a few seconds. (Background type also)
  2. Status Notification – Shows notification message and displayed till user action. (Background type also)
  3. Dialog Notification – Comes out of an active Activity.

(Background type) – is result of some background Service event that may not be related to current activity. That is, we can use this notification type in Service also, added to Activity.

1. Toast Notification

This type of notification will be used when there is no need of user interaction on seeing this message. This message occupies a rectangular box which will fade in and fade out after some time. The size of the box depends on the message content.

Android Toast Notification

For example, when user creates an event using calendar application it will notify the user as “Event Created” after the create action is completed. Refer the image.

Toast notification is best suited for one way information to the use where we don’t expect any response. Toast message does not stop or disturb the current activity, just the message is shown in parallel.

Example for Android Toast Notification

Toast notification can be created from an Activity or Service. Toast is the class to be used as below,

Context appContext = getApplicationContext();
Toast mailMessage = Toast.makeText(appContext, “Email Received.”, Toast. LENGTH_LONG);
mailMessage.setGravity(Gravity.TOP, 0, 0); //optional
mailMessage.show();

2. Status Notification

Status notification is used to display rich notification information especially from a (background) Service where user can interact. It will be shown as an icon with an alert in the status bar. When the user pulls down the status bar, the list of notification will be in the notification window.

Android Status Notification

For example when a SMS message is received a message icon is shown in the status bar. On pull down, the list of unread messages will be shown in the notification window.

Example shown in image: On snoozing the alarm, corresponding notification will be will be sent to the status bar with notification icon. A ticker message will be shown next to the icon for some time. In the image the clock icon represents the notification about the snooze event and the ticker message is shown next to the clock icon.

Android Status Notification Detail

  1. Create a simple notification with an icon alert. Alert can be a ticker text message or sound or vibration or flashlight.
  2. Associate notification message with details shown on message expansion to activity/intent. Notification message can be a list and it is identified using a unique identifier. Existing messages can be updated too.
  3. Register the notification message with notification manager. NotificationManager is a system service that manages all the notifications.

Example for Android Status Notification

//part 1 – notification icon alert
int icon = R.drawable.notification_icon;
// a ticker text message or sound or vibration or flashlight can be used for alert
CharSequence ticker = “Hi”;
long showAt = System.currenttimeMillis(); //immediately
Notification notification = new Notification(icon, ticker, showAt);
//part 2 – associate notification message with details shown on message expansion to activity/intent
CharSequence notificationTitle = "Notification:";
CharSequence notificationMessage = "SMS Received.";
Intent intent = new Intent(this, Activity.class);
PendingIntent objPendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Context ctx = getApplicationContext();
notification.setLatestEventInfo(ctx, notificationTitle, notificationMessage, objPendingIntent);
//part 3 – register the notification message with notification manager
private static final int notificationIdentifier = 101; //an unique number set by developer to identify a notification, using this notification can be updated/replaced
NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationIdentifier, objNotification);

Notification Alerts

Sound:


notification.defaults |= Notification.DEFAULT_SOUND;
//use the above default or set custom valuse as below
notification.sound = Uri.parse("file:///sdcard/notification/robo_da.mp3");

Vibration:


notification.defaults |= Notification.DEFAULT_VIBRATE;
//use the above default or set custom valuse as below
long[] vibrate = {0,200,100,200};
notification.vibrate = vibrate;

Flash Light:


notification.defaults |= Notification.DEFAULT_LIGHTS;
//use the above default or set custom valuse as below
notification.ledARGB = 0xffff0000;//red color
notification.ledOnMS = 400;
notification.ledOffMS = 500;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;

3. Dialog Notification

Dialog notification is not an exact type of notification. Dialog is common in window based UIs. A small panel that appears on top of an active window and user will not be able to do any other activity other than acting on the dialog. This is same here too. From an android Activity a dialog will be launched and the Activity loses focus. User should give input and work on the dialog. Once the user action is completed the dialog is closed. Dialog has many uses and one among them is notification to user.

Android Dialog Notification

For example we can show a progress bar which is a notification to user. We can ask for confirmation ‘yes’ or ‘no’ from user and this is a type of notification. For all these purposes dialog notification is used. There are many types of dialogs available such as,

Soon, I will write a separate detailed tutorial for Android dialogs.

Comments on "Android Notifications"

  1. Anitha says:

    Thank you. Simply fantastic. Please write more on Android. Loving javapapers!

  2. ayan says:

    :)
    Good one….
    short n simple…..

  3. Santhosh Reddy says:

    Very good. Thanks

  4. Rashi Coorg says:

    Thanks a lot! Very nice one.. :)

  5. pankaj says:

    fantastic and easy learning with snacks…

  6. satish says:

    DatePicketDialog ==> DatePickerDialog :)

  7. Joe says:

    Thanks Satish. Fixed it :-)

  8. Chaudhary says:

    Good explaination, it is easily understandable. Keep it up Joe..:)

  9. steve says:

    thank you for your writings which you send out. Feel very new and behind in this technology. Where to find more info for writing droid apps?
    thanks

  10. Joe says:

    @Steve,

    Android’s official tutorial is the best place to start with.

  11. Vivek Sahay says:

    Sir,
    I am using HTC Explorer mobile.I am having problem in installing phone updates as it is running shortage of INTERNAL MEMORY.There are few apps which are installed in the internal memory and are important,they are also not movable to SD Card.I have installed Facebook for android which take s a lot of space but there is also a facebook for HTC Sense internal application.What shall I do ???Kindly help.

  12. Vivek Sahay says:

    Sir,
    Is there any application for HTC Explorer using which I could move my important sms to a separate folder as we use to do in any ordinary phone…..??

  13. seshu says:

    ya its good, but how can we add this app to real time applications? like i want to set a alarm to particular time and i want to display notification to that time. any one help for this problem.

  14. […] DatePicker 11/10/2012Recently I wrote a tutorial on android notifications and there we touched dialog notifications. Android’s date picker is a type of dialog. This […]

  15. Goks says:

    Best tuturial Sir,
    can you give example of TextToSpeech converter?
    thanx in advance.

  16. Abdul Rahman says:

    Hello Sir,
    I having one query, instead of your coding – “Context appContext = getApplicationContext();
    Toast mailMessage = Toast.makeText(appContext, “Email Received.”, Toast. LENGTH_LONG);
    mailMessage.setGravity(Gravity.TOP, 0, 0); //optional
    mailMessage.show();”.

    Regularly i am using the following code:
    Toast.makeText(MainActivity.this,”Email Received…”,Toast.LENGTH_LONG).show();

    Which one is best and why?

    My Second query is what exactly “Context appContext = getApplicationContext();” this code means?

    Waiting for your reply, forward thanks…

  17. Praveen m.p says:

    Thanks very much

  18. alia says:

    hey could you tell me something about writing an android service .How do i attach an android service with an android application

  19. Ganesh says:

    Thank you .very nice example.

  20. sangeetha says:

    superb….. Thank You….

  21. Alex Pandy says:

    Hi the Status Notification is working but it is visible even after my action is done how to make it invisible once I see the activity

  22. rahul malhotra says:

    hello sir, suprb examples on your blog vry nice.
    i learnt much more here,thank you.

  23. rahul malhotra says:

    hello sir, suprb examples on your blog vry nice.
    i learnt much more from here,thank you.

  24. Sowmya says:

    Hello Joe, Please also write about PUSH notifications to GCM to Android using eclipse

  25. Joe says:

    Sure Sowmya, I have added the topic to my to do list.

  26. prachi says:

    this code is not working

  27. Siddharth Agaja says:

    Hello sir,
    I want to create statuse notification in my application, when new update is available on mysql database.so please sir help me in this i am new in android.
    waiting for your reply..

  28. monika says:

    sir can u lease help me.. i have problem that how to trigger notification at specific time everyday even app is closed

  29. gadha says:

    Hi SIR,

    CAN YOU PLEASE EXPLAIN HOW TO GET NOTIFICATION ON A SPECIFIC TIME.I am using timepicker to select the time.i need notification on the selected time.Now i am getting notification on current time.

    Thanks in andvance….

Comments are closed for "Android Notifications".