HTML5 desktop notifications, use of notification

Source: Internet
Author: User

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:

    1. dir: The direction of the text; its value can be auto(自动) , ltr(从左到右) , or rtl (right-to-left)
    2. lang: Specifies the language used in the notification.
    3. body: Additional strings displayed in the notification
    4. tag: Give the notification an ID to refresh, replace, or remove the notification when necessary.
    5. icon: The URL of a picture that will be used to display the notification icon.

The available events are as follows:

  • Notification.onclickHandles handling click of events. Triggered whenever a user clicks on a notification.
  • Notification.onshowHandles handling show of events. triggered when the notification is displayed.
  • Notification.onerrorHandles handling error of events. Triggered whenever an error is encountered by the notification.
  • Notification.oncloseHandles 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

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.