Saturday, September 8, 2012

Android Notifications Syestem

Android Notifications System is a very good feature in android OS. This provide user with the very recent notifications to the user each time when notification activity is called. A status notification system is added an icon to the status bar with a ticker text messages. When user click or touch the ticker android fires an Intent This activity is called the notification. Having  a full description or link of the other activity. You can also configure the notification to alert the user with a sound, a vibration, and flashing lights on the device.

Android notification is used to run a background service of any applications to run in the background and It never call its activity by itself human interaction is needed to perform any operation on the notifications. After clicking on it It will fire an other activity.
Notification bar is something look like this..

                                        Figure:1 Notification status bar

                                    Figure:2 Notification Bar with message

Notification can be provided to the user by any of the following ways.
  • ·         Status Bar Notification
  • ·         Vibrate
  • ·         Flash lights
  • ·         Play a sound
 While the notification is generated on the applications then it will only allow the user to interact with the other activity.

To create any notification we just needed two classes.
  • Notification - This class having all the property related to the notification status bar like what icon we have to show on the notification bar and how much time the notification will show means how much duration the notification should show.
  • NotificationManager - Thsi class an android system service that executes and manages all notifications. Hence you cannot create an instance of the NotificationManager but you can retrieve a reference to it by calling the getSystemService() method.
After using all these we just need to pass the notification object to the notify()  function this will run your notification.

While all these we have done now we have to make the activity where we have to define that what information we have to show on the activity. This is done by calling the method setLatestEventInfo() on the notification object.

Now let us see the example which i have given over here you can download the example  source code by just clicking here.

How the example is working--

Step 1: Make object for NotificationNanager.

            private NotificationManager myNotificationManager;
myNotificationManager =

Step 2: Create a notification object along with properties to display on the status bar like what image should display and duration for display

final Notification notifyDetails =
new Notification(,"I am Alert, Click me!",System.currentTimeMillis());

Step 3:  In the details I have added the intent which will transfer you to the google website by the browser

Context context = getApplicationContext();
CharSequence contentTitle = "Notification Details...";
CharSequence contentText = "Browse google Site by clicking me";
Intent notifyIntent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse(""));
PendingIntent intent =
      PendingIntent.getActivity(SimpleNotification.this, 0,
      notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
Step 4: Now the stage is set. Notify.
      myNotificationManager.notify(NOTFICATION_ID, notifyDetails);

Note that all of the above actions(except getting a handle to the NotificationManager) are done on the click of a button “Start Notification”. So all the details go into the setOnClickListener() method of the button.
Similarly, the notification, for the example sake is stopped by clicking a cancel notification button. And the code there is :

Now, you may realize that the constant NOTIFICATION_ID becomes the way of controlling, updating, stopping a current notification that is started with the same ID.

For more options like canceling the notification once the user clicks on the notification or to ensure that it does not get cleared on clicking the “Clear notifications” button, please see the android reference documentation.