Home / Android

Status Bar Notification in Android

Rating: 4.0Blog-star
Views: 12485
by Ruchitha Geebu
Last modified: January 25th 2021

Status Bar Notification

This Notification on Android can be done in any of the following ways:

  • Status Bar Notification
  • Vibrate
  • Flashlights
  • Play a sound

If you would like to Enrich your career with a  Android certified professional, then visit Mindmajix - A Global online training platform:  “Android Training  Course.  This course will help you to achieve excellence in this domain.

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.

Checkout:- Toast Notifications in Android

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.

                                                                             NotificationManager.

Creating a Notification:

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:

  • An icon for the status bar
  • A title and message, unless you define a custom notification layout
  • A PendingIntent, to be fired when the notification is selected

Optional settings for the status bar notification include:

  • A ticker-text message for the status bar
  • An alert sound
  • A vibrate setting
  • A flashing LED setting

Updating The Notification:

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.)

Checkout Android Interview Questions

Adding a sound:-

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;

Adding vibration:-

You can alert the user with the default vibration pattern or with a vibration pattern defined by your application.

notification.defaults |= Notification.DEFAULT_VIBRATE;

Adding flashing lights:-

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;

 

Explore Android Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

About Author

author
NameRuchitha Geebu
Author Bio

I am Ruchitha, working as a content writer for MindMajix technologies. My writings focus on the latest technical software, tutorials, and innovations. I am also into research about AI and Neuromarketing. I am a media post-graduate from BCU – Birmingham, UK. Before, my writings focused on business articles on digital marketing and social media. You can connect with me on LinkedIn.