1 First determine if the browser supports: window. Notification
2 Determine whether the browser is prompted for permission: notification.permission = = = ' granted ' (set to allow if not allowed: notification.requestpermission ())
3 Setting the content of the hint: var notification = new Notification (' order prompt ', {body: "You have a new order:" + Data + "});//notification (" title "," Content ")
4 can set other functions such as message click event: Notification.onclick = function (e) {
Alert ("click")
}
Cases:
if (window. Notification) { if (notification.permission = = = ' granted ') { var Notification = new Notification (' title ', {body: "Content You can define it yourself "}"); Notification.onclick = function (e) { alert ("Clicked on Me")//can click to open a URL } } else { Notification.requestpermission ();//set Allow notification } }
Expansion: Content can be set as follows:
dir
: The direction of the text; its value can be auto(自动)
, ltr(从左到右)
, or rtl
(right-to-left)
lang
: Specifies the language used in the notification.
body
: Additional strings displayed in the notification
tag
: Give the notification an ID to refresh, replace, or remove the notification when necessary.
icon
: The URL of a picture that will be used to display the notification icon.
The available events are as follows:
Notification.onclick
Handles handling click
of events. Triggered whenever a user clicks on a notification.
Notification.onshow
Handles handling show
of events. triggered when the notification is displayed.
Notification.onerror
Handles handling error
of events. Triggered whenever an error is encountered by the notification.
Notification.onclose
Handles handling close
of events. Triggered when the user closes the notification.
To turn off notification instance methods:
var n = new Notification (thetitle,options); SetTimeout (N.close.bind (n), 4000);
Note: IE does not support, I was tested successfully in Google and Firefox.
The development of their own time to record, the big God passing the words please ignore or advise what need to pay attention to points, thank you.
HTML5 desktop notifications, use of notification