Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze Notification comprehensively

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly analyzes how to comprehensively analyze the relevant knowledge points of Notification, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "how to fully analyze Notification".

The frequency of Notification used in Android can be said to be very high. In this article, I will analyze all aspects of Notification to make you have a better understanding of Notification.

Steps for using Notification

1. Get NotificationManager

NotificationManager mNotificationManager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE)

two。 Create NotificationCompat.Builder

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder (this)

3. Set some Notification-related properties on Builder:

MBuilder.setContentTitle ("title") / / set notification bar title .setContentText ("content") / set notification bar display .setContentIntent (getDefalutIntent (Notification.FLAG_AUTO_CANCEL)) / / set notification bar Click intention / .setNumber (number) / / set the number of notification collections .setTicker ("Notification arrives") / / Notification * * appears in the Notification Bar The time when the notification is generated by .setWhen (System.currentTimeMillis ()) / / with rising animation is displayed in the notification message, usually the time obtained by the system. Setpriority (Notification.PRIORITY_DEFAULT) / / set the priority of the notification / / .setAutoCancel (true) / / set this flag when the user clicks on the panel so that the notification will be automatically cancelled .setOngoing (false) / / ture Set him as an ongoing notification. They are usually used to indicate a background task in which the user is actively involved (such as playing music) or waiting in some way, thus occupying devices (such as a file download, synchronization, active network connection) .setDefaults (Notification.DEFAULT_VIBRATE) / / the simplest and most consistent way to add sound, flashing and vibration effects to a notification is to use the current user default settings and use the defaults property You can combine / / Notification.DEFAULT_ALL Notification.DEFAULT_SOUND to add sound / / requires VIBRATE permission .setSmallIcon (R.drawable.ic_launcher) / / set notification mini icon

4. Create notifications using Builder

Notification notification = mBuilder.build ()

5. Use NotificationManager to push notifications out

Int id = 1999; LogUtils.d (TAG, "create Notification"); mNotificationManager.notify (id, notification)

Analysis of important methods of Notification

The basic operations of Notification are to create, update and cancel. There are three necessary properties for a Notification, and if not set, an exception will be thrown at run time:

Small icons, set by the setSmallIcon () method

Title, set by the setContentTitle () method

Content, set by the setContentText () method

Except for the above three items, all others are optional. Even so, you should set an Action for Notification so that you can jump directly to an Activity in App, start a Service, or send a Broadcast. Otherwise, Notification can only serve as a notification, not interact with users.

When the system receives the notification, it can be reminded by many ways, such as vibration, ringing, breathing lights and so on.

1) setSmallIcon () and setLargeIcon ()

There are two ways to set the size icon for notifications in NotificationCompat.Builder. What's the difference between the two methods? When setSmallIcon () and setLargeIcon () exist at the same time, smallIcon is displayed in the lower-right corner of largeIcon; when only setSmallIcon () is set, smallIcon is displayed on the left. Look at the picture and you'll see. For some ROM, the source code may be modified, for example, there is no difference between large icons and small icons notified on MIUI.

The Google official interprets the setSmallIcon () method as follows:

Set the small icon resource, which will be used to represent the notification in the status bar. The platform template for the expanded view will draw this icon in the left, unless a large icon has also been specified, in which case the small icon will be moved to the right-hand side.

2) set the reminder marker Flags

Method explanation: reminder markers, add sound, flashing lights and vibration effects to the notification to achieve the notification reminder effect, you can combine multiple attributes

A) after creating the notification bar, assign a value by adding the .flags attribute to it.

Notification notification = mBuilder.build (); notification.flags = Notification.FLAG_AUTO_CANCEL

B) set the corresponding flags through the intention in the setContentIntent (PendingIntent intent) method

Public PendingIntent getDefalutIntent (int flags) {PendingIntent pendingIntent= PendingIntent.getActivity (this, 1, new Intent (), flags); return pendingIntent;}

Introduction of each marker

Notification.FLAG_SHOW_LIGHTS / / tricolor light reminder, when using tricolor light reminder, you must add this symbol Notification.FLAG_ONGOING_EVENT / / initiate a running event (active) Notification.FLAG_INSISTENT / / let the sound and vibration * * cycle until the user responds (cancels or opens) Notification.FLAG_ONLY_ALERT_ONCE / / initiates Notification Both ring tones and vibrations are executed only once Notification.FLAG_AUTO_CANCEL / / automatically disappears after the user clicks the notification Notification.FLAG_NO_CLEAR / / Notification will be cleared only when all are cleared. It is not clear that the notification (the notification of QQ cannot be cleared) Notification.FLAG_FOREGROUND_SERVICE / / indicates the service that is running

3) .setDefaults (int defaults) (a method in NotificationCompat.Builder that sets how to prompt when a notification arrives)

Method explanation: the easiest way to add sound, flashing lights and vibration effects to a notification is to use the default (defaults) attribute, which can be combined with multiple properties (the same as the prompt in method 1)

Corresponding attributes:

Notification.DEFAULT_VIBRATE / / VIBRATE permission is required to add a default vibration reminder

Notification.DEFAULT_SOUND / / add default sound reminder

Notification.DEFAULT_LIGHTS// adds a default tricolor reminder

Notification.DEFAULT_ALL// adds all the above three reminders by default

/ * displays notifications with default ringtone, vibration, and breath lamp effects * if you need to achieve a custom effect Please refer to the next three examples * / private void showNotifyWithMixed () {NotificationCompat.Builder builder = new NotificationCompat.Builder (this) .setSmallIcon (R.mipmap.ic_launcher) .setContentTitle ("I am the notification of ringtone + vibration + breath light effect") .setContentText ("the library is the same as setDefaults (Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)" .setDefaults (Notification.DEFAULT_ALL); mManager.notify (5, builder.build ());}

4) setVibrate (long [] pattern)

Method explanation: set the time of vibration

.setVibrate (new long [] {0300500700})

Effect: delay 0ms, then vibrate 300ms, then delay 500ms, then vibrate 700ms.

There is another way to write it:

MBuilder.build () .vibrate = new long [] {0300500700}

If you want to set the default vibration mode, set it to DEFAULT_VIBRATE by default in method (2).

Example:

/ * to show a notice with vibration effect, you need to apply for vibration permission in AndroidManifest.xml * * add: when testing vibration, the mode of the phone must be set to ringtone + vibration mode, otherwise you will not feel the vibration * / private void showNotifyWithVibrate () {/ / there are also two ways to set the vibration. Just like setting the ring tone, I will not repeat long [] vibrate = new long [] {0500, 1000 here. 1500} NotificationCompat.Builder builder = new NotificationCompat.Builder (this) .setSmallIcon (R.mipmap.ic_launcher) .setContentTitle ("I am the notification with vibration effect") .setContentText ("tremble, mortal ~") / / use the system default vibration parameters Will conflict with custom / / .setDefaults (Notification.DEFAULT_VIBRATE) / / Custom Vibration effect .setVibrate (vibrate) / / another way to set vibration / / Notification notify = builder.build (); / / call system default vibration / / notify.defaults = Notification.DEFAULT_VIBRATE; / / call self-set vibration / / notify.vibrate = vibrate; / / mManager.notify (3); mManager.notify (3, builder.build ());}

4) method: .setLights (intledARGB, intledOnMS, intledOffMS)

Method explanation: android supports tricolor light reminder, this method is to set different color lights in different scenes.

Description: where ledARGB represents the light color, the duration of ledOnMS light, and the time when ledOffMS is dark.

Note:

1) tricolor reminders are supported only when the marker Flags is set to Notification.FLAG_SHOW_LIGHTS.

2) the color here is related to the equipment, not all colors are OK, it depends on the specific equipment.

Notification notify = mBuilder.build (); notify .setLights (0xff00eeff, 500200)

In the same way, the following methods can set the same effect:

Notification notify = mBuilder.build (); notify.flags = Notification.FLAG_SHOW_LIGHTS; notify.ledARGB = 0xff00eeff; notify.ledOnMS = 500; notify.ledOffMS = 400

If you want to use the default tricolor reminder, set the method (2) to DEFAULT_LIGHTS by default.

Example:

/ * displays a notice with the effect of a breathing light, but I don't know why The test here did not succeed * / private void showNotifyWithLights () {final NotificationCompat.Builder builder = new NotificationCompat.Builder (this) .setSmallIcon (R.mipmap.ic_launcher) .setContentTitle ("I am the notification with the effect of breathing lamp") .setContentText ("twinkle ~") / / ledARGB indicates the light color, duration of ledOnMS, LedOffMS dark time. SetLights (0xFF0000 3000, 3000) Notification notify = builder.build (); / / breath light reminders are supported only if the marker Flags is set to Notification.FLAG_SHOW_LIGHTS. Notify.flags = Notification.FLAG_SHOW_LIGHTS; / / another way to set the lights parameter / / notify.ledARGB = 0xFF0000; / / notify.ledOnMS = 500; / / notify.ledOffMS = 5000; / / use handler to delay sending notifications, because the breathing light will always be on Handler handler = new Handler () when the usb is connected Handler.postDelayed (new Runnable () {@ Override public void run () {mManager.notify (4, builder.build ());}, 10000);}

5) method: .setSound (Uri sound)

Method explanation: set the default or custom ringtone to remind you.

/ / get default ringtones .setDefaults (Notification.DEFAULT_SOUND) / / get custom ringtones .setSound (Uri.parse ("file:///sdcard/dance.mp3")) / / get ringtones .setSound (Uri.withAppendedPath (Audio.Media.INTERNAL_CONTENT_URI," 5 ") in Android multimedia library)

In the same way, another setting method with the same effect is not discussed here, which is the same as the one above.

Example:

/ * Notification with custom ringtone effect * add: use the ringtone effect that comes with the system: Uri.withAppendedPath (Audio.Media.INTERNAL_CONTENT_URI, "6") * / private void showNotifyWithRing () {NotificationCompat.Builder builder = new NotificationCompat.Builder (this) .setSmallIcon (R.mipmap.ic_launcher) .setContentTitle ("I am a notification with ringtone effect") .setContentText ("is it wonderful? Quietly listen to ~ ") / / the calling system rings by default. When this property is set, setSound () will be invalid / / .setDefaults (Notification.DEFAULT_SOUND) / / call the ringtone in the system's multimedia trousers / / .setSound (Uri.withAppendedPath (MediaStore.Audio.Media.INTERNAL_CONTENT_URI," 2 ")). / / call the self-provided ring tone, located in the / res/values/raw directory. SetSound (Uri.parse ("android.resource://com.littlejie.notification/" + R.raw.sound)); / / another way to set the ring tone / / Notification notify = builder.build (); / / call the system default ring tone / / notify.defaults = Notification.DEFAULT_SOUND / / call the self-provided ringtone / / notify.sound = Uri.parse ("android.resource://com.littlejie.notification/" + R.raw.sound); / / call the ringtone that comes with the system / / notify.sound = Uri.withAppendedPath (MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "2"); / / mManager.notify (2); mManager.notify (2, builder.build ());}

6) method: .setPriority (int pri)

Method explanation: set the priority (it is not very useful in the actual project, and setting the * level will not cause your notification bar to appear in the * bit)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report