Desktop Notification, also known as Web Notification, notifies users that an event has occurred in the form of a pop-up desktop dialog box outside the Web page. Web notification became the recommended standard of the WWW in 2015.9.10, the website https://www.w3.org/TR/notifications/. Each notification dialog box includes title, direction, language, and origin. The notification dialog box can also have body, tag, icon URL and icon image.
The notification must obtain the authorization of the user to be able to display, thereby avoiding user expectations of the notification interference. There are three authorizations for notifications, respectively, by the string "default" (user not explicitly authorized, with denied), "denied", "granted".
Because the display of notifications is related to the implementation of the browser and is related to the user's authorization, it is often appropriate to check the authorization of the browser and user before using desktop Alerts, as shown in the following example:
function Checknotification () {
if (! () Notification "in Window") {
Alert ("This browser does not support desktop notification");
}
Check whether notification permissions have alredy been granted
else if (notification.permission = = "Granted") {
If It ' s okay let ' s create a notification
New Notification ("granted!");
}
Otherwise, ask the user for permission
else if (notification.permission!== ' denied ') {
Notification.requestpermission (function (permission) {
If the user accepts, let ' s create a notification
if (Permission = = "Granted") {
New Notification ("Request granted!");
}
});
}
}
The Chrome chrome.notifications.* API can create desktop alerts based on templates and display notifications in the tray in the lower-right corner of the operating system.
To use notifications in the Chrome browser extensions, first declare the notifications permissions in the Manifest.json file as follows:
{
"Permissions": [
"Notifications"
],
}
Chrome.notifications.TemplateType is an enumeration type with the following enumeration values:
enumeration value |
Comments |
"Basic" |
Default templates icon, title, message, Expandedmessage located on two button |
"Image" |
icon, title, message, Expandedmessage, image located on two button |
"List" |
icon, title, message, items located above two button |
"Progress" |
icon, title, message, progress located on two button |
Chrome.notifications. Permissionlevel is an enumeration type with the following enumeration values:
enumeration value |
Comments |
"Granted" |
Default, user authorization display notification |
"Denied" |
User not authorized to display notification |
The properties of the Chrome.notifications.NotificationOptions object are as follows:
Property name |
Type |
Required/Optional |
Comments |
Type |
Templatetype |
Optional |
Type of notification, Chrome.notifications.create () required to create notification |
Title |
String |
Optional |
The title of the notice, Chrome.notifications.create () required to create notification |
Message |
String |
Optional |
The subject content of the notification, Chrome.notifications.create () required to create notification |
Contextmessage |
String |
Optional |
Optional content of the notification |
Buttons |
Array of Object |
Optional |
The array has a maximum of 2 elements, representing 2 button respectively. Each button object has title and Iconurl two attributes (string types), where the Iconurl property is optional |
Appiconmaskurl |
String |
Optional |
The mask that applies the icon URL to the specification URL |
Iconurl |
String |
Optional |
The URL of the icon |
ImageUrl |
String |
Optional |
URL of a picture of a notification of type ' image ' |
Priority |
Integer |
Optional |
Priority, valid range [ -2,2], default 0 |
Eventtime |
Double |
Optional |
Notice of the time stamp, unit ms |
Items |
Array of Object |
Optional |
Each element in the array represents a notification. Each notification object has the title and message two attributes (String type) |
Progress |
Integer |
Optional |
Current progress, effective range [0,100] |
Isclickable |
Boolean |
Optional |
Whether the notification window responds to a click event |
Common methods in the Chrome.notifications API:
· Create and display notifications
Chrome.notifications.create (
String Notificationid,
Notificationoptions options,
function (string Notificationid) {...}
)
The properties are described below:
Property name |
Type |
Required/optional |
Comment |
Notificationid |
String |
Optional |
identifier for the notification. Generates a unique identifier automatically if it is not set or set to, If the value set is the same as the existing notification, replace the existing notification |
Options |
Notificationoptions |
Must be selected |
|