Home  >  Article  >  Web Front-end  >  Examples of H5 implementing desktop notification and prompt functions

Examples of H5 implementing desktop notification and prompt functions

黄舟
黄舟Original
2017-10-16 11:14:002853browse

This article mainly introduces the implementation of desktop notification prompt function in HTML5. It is very good and has reference value. Friends who need it can refer to it

The specific code is as follows:


<button id="button">有人想加你为好友</button>
<p id="text"></p>
;(function (){
    if(window.Notification){
        var btn = document.getElementById("button");
        var txt = document.getElementById("text");
        btn.onclick = function (){
            if(Notification.permission == "granted"){
                popNotice();
            }else if(Notification.permission != "denied"){
                Notification.requestPermission().then(function (permission){
                    popNotice()
                })
            }
        };
        function popNotice(){
            if(Notification.permission == "granted"){
                var notification = new Notification("你好:",{
                    body:"请问今晚有空吗",
                    icon:"http://image.zhangxinxu.com/左边头像地址"
                });
                notification.onclick = function (){
                    txt.innerHTML = new Date().toTimeString().split(" ")[0]+"收到信息";
                    notification.close();
                }
            }
        }
    }else{
        console.log("浏览器不支持Notification");
    }
})();

Notification.requestPermission() is a request that allows the browser to prompt whether to allow notifications. It returns "3" values ​​like Notification.permission. "granted" "default" "denied" The user allows notifications, the user There is no control yet, users dislike it

Summary

The above is the detailed content of Examples of H5 implementing desktop notification and prompt functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn