[Android] Status bar notification Notification, NotificationManager detailed explanation

In the Android system, it is very convenient to send a status bar notification. Let’s take a look at how to send status bar notifications, and what parameters can be set for status bar notifications?

First, to send a status bar notification, two classes must be used: NotificationManager and Notification.

NotificationManager: It is the management class for status bar notifications, responsible for sending notifications, clear notifications, etc.

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification: is a specific status bar notification object, you can set parameters such as icon, text, prompt sound, vibration, etc.

The following are the basic parameters required to set up a notification:

  • An icon (the notification icon)
  • A title and expanded message (the title and content of the notification)

Optional settings:

  • A ticker-text message (a message at the top of the status bar)
  • An alert sound
  • A vibrate setting (vibrate)
  • A flashing LED setting
  • and many more

  • Create Notification

Notification is started by the notify(int, Notification) method of NotificationManager.

The first parameter uniquely identifies the Notification, and the second parameter is the Notification object.

  1. Update Notification

Call Notification’s setLatestEventInfo method to update the content, and then call NotificationManager’s notify() method. (See the example below for details)

  1. Delete Notification

  2. Notification settings (vibration, ringtone, etc.)

  3. Basic settings:

// Create a new status bar notification baseNF = new Notification(); // Set the icon displayed in the status bar for notifications baseNF.icon = R.drawable.icon; // The content displayed in the status bar during notification baseNF.tickerText = “You clicked BaseNF!”; // Default parameters for notifications DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS. // If you want to use all default values, use DEFAULT_ALL. // Use the default sound here baseNF.defaults = Notification.DEFAULT_SOUND; // The second parameter: The message title displayed when the status bar is pulled down expanded message title // The third parameter: the content of the message displayed when the status bar is pulled down expanded message text // The fourth parameter: the page jump is performed when the notification is clicked baseNF.setLatestEventInfo(Lesson_10. this, “Title01”, “Content01”, pd); // Issue a status bar notification //The first parameter is the unique ID for the Notification // and the second is the Notification object. nm.notify(Notification_ID_BASE, baseNF);

With a picture description:

  1. Add sound

If you want to use the default sound, just use default.

baseNF.defaults = Notification.DEFAULT_SOUND;

If you want to use a custom sound, then you need to use sound. as follows:

notification.sound = Uri.parse(“file:///sdcard/notification/ringer.mp3”);

The above method uses your own ringtone. If you want to use the system’s own ringtone, you can do this:

notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, “6”);

It should be noted that if default and sound appear at the same time, then sound is invalid and the default ringtone will be used.

By default, the notification sound plays once and ends. If you want the sound to loop, add FLAG_INSISTENT to the flags parameter. In this way, the sound will not end until the user responds, such as pulling down the status bar.

notification.flags |= notification.FLAG_INSISTENT;

  1. Add vibration

If the default vibration mode is used, then the default is also used.

notification.defaults |= Notification.DEFAULT_VIBRATE;

Of course, you can also define the vibration form yourself, here you need to use a Long type array.

long[] vibrate = {0,100,200,300}; notification.vibrate = vibrate;

In the Long-type array here, the first parameter is the time to wait before starting to vibrate, the second parameter is the time of the first vibration, the third parameter is the time of the second vibration, and so on. How long the array is. But with this method, there is no way to repeat the vibration.

Similarly, if default and vibrate appear at the same time, the default form will be used.

Another thing to note: permission is required to use the vibrator, as follows:

  1. Flash

Use the default lights, as follows:

notification.defaults |= Notification.DEFAULT_LIGHTS;

customize:

notification.ledARGB = 0xff00ff00; notification.ledOnMS = 300; notification.ledOffMS = 1000; notification.flags |= Notification.FLAG_SHOW_LIGHTS;

Where ledARGB represents the light color, ledOnMS bright duration, ledOffMS dark time.

Note: The colors here are related to the device, not all colors are available, it depends on the specific device.

  1. Other useful settings:

flags:

Notification.FLAG_INSISTENT; // let the sound and vibration loop infinitely until the user responds

Notification.FLAG_AUTO_CANCEL; // After the notification is clicked, it will disappear automatically

Notification.FLAG_NO_CLEAR; // When I click ‘Clear’, I don’t know the notification (QQ’s notification cannot be cleared, this is the one used)

Below is an example I made for your reference. It includes creating notifications, updating notifications, clearing notifications, setting custom ringtones, customizing vibrations, customizing notification views, and more.

Attached code:

Main class:

defaults |= Notification.DEFAULT_LIGHTS; // Let the sound and vibration loop infinitely until the user responds baseNF.flags |= Notification.FLAG_INSISTENT; // After the notification is clicked, it will disappear automatically baseNF.flags |= Notification.FLAG_AUTO_CANCEL; // Click ‘ When Clear’, the notification is not clear (QQ’s notification cannot be cleared, this is the one used) baseNF.flags |= Notification.FLAG_NO_CLEAR; // The second parameter: the message title displayed when the status bar is pulled down expanded message title // The first Three parameters: the message content displayed when the status bar is pulled down expanded message text // The fourth parameter: perform page jump when the notification is clicked baseNF.setLatestEventInfo(Lesson_10.this, “Title01”, “Content01”, pd); / / Send a status bar notification//The first parameter is the unique ID for the Notification // and the second is the Notification object. nm.notify(Notification_ID_BASE, baseNF); break; case R.id.le10bt02: // update notification/ / For example, the status bar prompts that there is a new text message, but before you can check it, another message prompts you. // At this time, the method of updating the original notification is used to compare. //(You can also send a new notification, but this will cause confusion in the notification, and display multiple notifications to the user, which is not user-friendly) baseNF.setLatestEventInfo(Lesson_10.this, “Title02”, “Content02”, pd ); nm. notify(Notification_ID_BASE, baseNF); break; case R.id.le10bt03: // clear baseNF nm.cancel(Notification_ID_BASE); break; case R.id.le10bt04: mediaNF = new Notification(); mediaNF.icon = R.drawable .icon; mediaNF.tickerText = “You clicked MediaNF!”; // custom sound mediaNF.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, “6”); // vibration on notification // first Parameters: the time to wait before vibrating // The second parameter: the duration of the first vibration, and so on long[] vir = {0,100,200,300}; mediaNF.vibrate = vir; mediaNF.setLatestEventInfo(Lesson_10.this, “Title03” , “Content03”, pd); nm.notify(Notification_ID_MEDIA, mediaNF); break; case R.id.le10bt05: // clear mediaNF nm.cancel(Notification_ID_MEDIA); break; case R.id.le10bt06: nm.cancelAll( ); break; case R.id.le10bt07: // Customize the drop-down view, such as the progress bar displayed when downloading software. Notification notification = new Notification(); notification.icon = R.drawable.icon;

home page:

Custom view page:

So much to say.

Leave a Comment

Your email address will not be published. Required fields are marked *