This Notification on Android can be done in any of the following ways:
To create a status bar notification, you will need to use two classes: Notification and NotificationManager.
Notification – defines the properties of the status bar notification like the icon to display, the test to display when the notification first appears on the status bar and the time to display.
NotificationManager – is 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.
Once you procure this handle, you invoke the notify() method on it by passing the notification object created.
So far, you have all the information to display on the status bar. However, when the user clicks the notification icon on the status bar, what detailed information should you show the user? This is yet to be created. This is done by calling the method setLatestEventInfo() on the notification object.
A Notification object defines the details of the notification message that is displayed in the status bar and notifications window, and any other alert settings, such as sounds and blinking lights.
A status bar notification requires all of the following:
Optional settings for the status bar notification include:
You can update the information in your status bar notification as events continue to occur in your application. For example, when a new SMS text message arrives before previous messages have been read, the Messaging application updates the existing notification to display the total number of new messages received. This practice of updating an existing notification is much better than adding new notifications, because it avoids clutter in the notifications window.
Because each notification is uniquely identified by the NotificationManager with an integer ID, you can revise the notification by calling setLatestEventInfo() with new values, change some field values of the notification, and then call notify() again.
You can revise each property with the object member fields (except for the Context and the notification title and text). You should always revise the text message when you update the notification by calling setLatestEventInfo() with new values for contentTitle and contentText. Then call notify() to update the notification. (Of course, if you’ve created a custom notification layout, then updating these title and text values has no effect.)
You can alert the user with the default notification sound (which is defined by the user) or with a sound specified by your application.
To use the user’s default sound, add “DEFAULT_SOUND” to the defaults field:
notification.defaults |= Notification.DEFAULT_SOUND;
You can alert the user with the default vibration pattern or with a vibration pattern defined by your application.
To use the default pattern, add DEFAULT_VIBRATE to the defaults field:
notification.defaults |= Notification.DEFAULT_VIBRATE;
To alert the user by flashing LED lights, you can implement the default light pattern (if available), or define your own color and pattern for the lights.
To use the default light setting, add DEFAULT_LIGHTS to the defaults field:
notification.defaults |= Notification.DEFAULT_LIGHTS;
Get Updates on Tech posts, Interview & Certification questions and training schedules