Android _ practical technology (1) -- Notification creation process and usage, and notification Creation

Source: Internet
Author: User

Android _ practical technology (1) -- Notification creation process and usage, and notification Creation

Android. app. Notification
Android. app. Notification. Builder

Certificate --------------------------------------------------------------------------------------------------------------------------------------

Notification, commonly known as Notification, is a global Notification that is displayed at the top of the screen. It first appears as an icon. When a user slides down, displays the specific content of the notification.

Icationicationmanager is an important system-level service. The object is located in the framework layer of the application. The application can send Global notifications to the system through it, which manages notifications.

In this case, you need to create a Notification object to carry the Notification content.

However, in actual use, we do not directly construct the Notification object, but use its internal class (Android 3.0 watershed ):

NotificationCompat. Builder to instantiate an object (after Android3.0) --- As old as API Level 4

Notification. Builder to instantiate an object (before Android 3.0) --- recommended by myself (officially recommended)

And set the various attributes of the notification (setXxx). If you are still using setLatestEventInfo (), I suggest you give up on it. AS is not recommended for this purpose, see the official documentation.

Finally, a Notification object is obtained through the. build () method.

After obtaining this object, you can use icationicationmanager. Y () to send notifications.

Certificate --------------------------------------------------------------------------------------------------------------------------------------

Icationicationmanager is a notification manager class, which is a service maintained by the system,It is obtained in singleton mode.Therefore, this object is not directly instantiated.

In the Activity, you can use the Activity. getSystemService (String) method to obtain the icationicationmanager object. The Activity. getSystemService (String) method can return the corresponding object through the Android system-level service handle. Here, icationicationmanager is returned, so you can directly pass Context. NOTIFICATION_SERVICE.

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notice:Although various attributes are provided in the notification, several attributes of a notification object must be set. Other attributes are optional. The required attributes are as follows:

~ Small icon, set using setSamllIcon () method.

~ Title, Which is set using setContentTitle.

~ Set the text content using setContentText.

 

[Update and remove Notification]

When you use icationicationmanager. Y () to send a notification, you must pass an identifier to uniquely identify the notification. In some scenarios, new notifications are not infinitely added, and sometimes the original Notification information needs to be updated. In this case, you can rewrite the build Notification and use the same identifier as the previous Notification to send notifications, at this time, the old notification will be replaced by the new notification to update the notification.

How can I cancel a notification after it is displayed on the status bar but used? Android provides usTwo TypesMethod removal notification:

(1) Notification is maintained by itself. You can use setAutoCancel () to set whether to maintain the Notification and pass a boolean data. Example: setAutoCanel (true)

(2) one is to use the icationicationmanager notification manager object for maintenance. It uses the specified notification Id to operate the notification when sending the notification through notify (). You can use cancel (int) to remove a specified notification. You can also use cancelAll () to remove all notifications.

 

[PendingIntent]
PendingIntent provides multiple static getXxx () methods for obtaining PendingIntent objects for different scenarios. Generally, several parameters to be passed are common. Only one flag parameter is introduced to identify the PendingIntent construction selection:

~ FLAG_CANCEL_CURRENT: If the built PendingIntent already exists, cancel the previous one and re-build one.

~ FLAG_NO_CREATE: if the previous PendingIntent does not exist, it will no longer be built.

~ FLAG_ONE_SHOT: indicates that the PendingIntent built here can only be used once.

~ FLAG_UPDATE_CURRENT: If the built PendingIntent already exists, replace it.

 

[Advanced Skills]

For some notifications, you need to call some device resources so that users can quickly discover new notifications. Generally, the configurable responses include:Ringtones, flashlights, and vibrations. For these three attributes, Notification. Builder provides three methods:

(1) setSound (Uri sound): Set a ringtone to respond to a notification. The Uri parameter is transmitted in the format of "file: // mnt/sdcard/Xxx.mp3 ".

(2) setLights (int argb, int onMs, int offMs): Set the flashing rate of the front-end LED light, lasting in milliseconds, and pausing in milliseconds.

(3) setVibrate (long [] pattern): sets the vibration mode and stores the vibrations at milliseconds in a long array.

Most of the time, we do not need to set a specific response. We only need to follow the system notification effect on the user's device. Then we can use setDefaults (int) to set the default response parameter, in Notification, the constant is defined for its parameters. We only need to use it:

SetDefaults (int ):

DEFAULT_ALL: The system defaults to ringtones, flashes, and vibrations.

DEFAULT_SOUND: the system's default ringtone.

DEFAULT_VIBRATE: default system vibration.

DEFAULT_LIGHTS: default system flash.

In Android, if you need to access a hardware device, you needAuthorizationSo you need to add two authorizations in the configuration file AndroidManifest. xml to grant the access to the vibrator and Flash respectively:

<Uses-permission android: name = "android. permission. VIBRATE"/>

<Uses-permission android: name = "android. permission. FLASHLIGHT"/>

 

[Notification visual style]

(1) Normal view standard view (for example, instance)

(2) Big view (available for reference)

 

[Instance]

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.