The status notification bar mainly involves two classes: Notification and icationicationmanager.
Notification is the notification information class, which corresponds to the attributes of the notification bar.
Icationicationmanager: Manages status bar notifications and is responsible for sending and clearing notifications.
Note:Icationicationmanager is a system service, so it must passGetsystemservice (icationication_service) method to obtainThe method is as follows.
NotificationManager mNotificationManager = (NotificationManager) Application.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
Create a notification
-① CreateNotificationcompat. BuilderBuilder constructor of the notification bar to set the UI information and behavior of the notification
-CallIcationicationcompat. Builder. Build ()To create a notification
-CallIcationicationmanager. Notify ()To send notifications
① Builder constructor of the notification bar
NotificationCompat.Builder builder = new NotificationCompat.Builder(MyApplication.getInstance())
② Set the UI information and behavior of the notification
Mbuilder. setcontenttitle ("test title") // set the title of the notification bar. setcontenttext ("Test content") // set the display content in the notification bar. setcontentintent (getdefalutintent (notification. flag_auto_cancel) // set the intent of clicking in the notification bar //. setnumber (number) // set the number of Notification sets. setticker ("") // The notification appears in the notification bar for the first time, with a rising animation effect. setwhen (system. currenttimemillis () // the time when a notification is generated. It is displayed in the notification information, generally the time obtained by the system. setpriority (notification. priority_default) // set the notification priority //. setautocancel (true) // set this flag. When the user clicks the panel, the notification will Call cancel. setongoing (false) // ture to set it to an ongoing notification. They are usually used to represent a background task. users actively participate (such as playing music) or are waiting in some way, so they occupy devices (such as downloading a file and performing synchronization operations, active Network connection ). setdefaults (notification. default_vibrate) // The simplest and most consistent way to add sound, flashing, and vibration effects to the notification is to use the default settings of the current user, and use the ults attribute to combine // notification. default_all notification. default_sound: Add a sound // requires vibrate permission. setsmallicon (R. drawable. ic_launcher); // sets the notification icon
Necessary notification content
A notification must contain:
-Use setsmallicon () to set a small icon
-Use setcontenttitle () to set a title.
-Set the details text using setcontenttext ().
Optional notification content and settings
-All other projects are optional. For more information, seeNotificationcompat. BuilderReferences
Notification Behavior
-Although it is optional, you should add at least one action for your notification.
-The behavior allows the user to directly switch to an activity in the application.
-Notifications can provide multiple actions:
> You should always define the actions triggered by user clicks. This behavior is usually used to open an activity.
> You can also add buttons in the notification to perform additional actions, such as delaying an alarm or rapidly replying to a text message. This feature is available from Android 4.1.
> If you use the additional action button, you must make these functions available during your activity.
-How to define behaviors?
> Behavior is defined by a pendingintent. Pendingintent contains an intent used to start the activity.
> To associate pendingintent with a gesture, callNotificationcompat. Builder. For example, if you want to start an activity when you click the notification content, you need to call the setcontentintent () method to add pendingintent.
References:
Integrated and comprehensive learning of Android notification bar notification (a demo lets you fully understand it)
Notificaiton status notification bar code explanation 1