In this tutorial we shall learn about different types of notifications in android. Following are the three types of android notifications,
(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.
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.
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.
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();
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.
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.
//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);
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;
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.
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 are closed for "Android Notifications".
Thank you. Simply fantastic. Please write more on Android. Loving javapapers!
:)
Good one….
short n simple…..
Very good. Thanks
Thanks a lot! Very nice one.. :)
fantastic and easy learning with snacks…
DatePicketDialog ==> DatePickerDialog :)
Thanks Satish. Fixed it :-)
Good explaination, it is easily understandable. Keep it up Joe..:)
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
@Steve,
Android’s official tutorial is the best place to start with.
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.
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…..??
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.
[…] 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 […]
Best tuturial Sir,
can you give example of TextToSpeech converter?
thanx in advance.
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…
Thanks very much
hey could you tell me something about writing an android service .How do i attach an android service with an android application
Thank you .very nice example.
superb….. Thank You….
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
hello sir, suprb examples on your blog vry nice.
i learnt much more here,thank you.
hello sir, suprb examples on your blog vry nice.
i learnt much more from here,thank you.
Hello Joe, Please also write about PUSH notifications to GCM to Android using eclipse
Sure Sowmya, I have added the topic to my to do list.
this code is not working
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..
sir can u lease help me.. i have problem that how to trigger notification at specific time everyday even app is closed
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….