Web Notification消息通知的使用(桌面消息通知)

如果沒有效果,請開啓瀏覽器的消息通知權限。  下面上代碼

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
  
 <button id="button">有人想加你爲好友</button>
<p id="text"></p>
 </body>
 
 <script type="text/javascript">  
if (window.Notification) {
    var button = document.getElementById('button'), text = document.getElementById('text');
    
    var popNotice = function() {
        if (Notification.permission == "granted") {
            var notification = new Notification("Hi,帥哥:", {
                body: '可以加你爲好友嗎?',
                icon: 'https://s2.ax1x.com/2019/08/19/m3hgYj.jpg'
            });
            
            notification.onclick = function() {
                text.innerHTML = '張小姐已於' + new Date().toTimeString().split(' ')[0] + '加你爲好友!';
                notification.close();    
            };
        }    
    };
    
    button.onclick = function() {
        if (Notification.permission == "granted") {
            popNotice();
        } else if (Notification.permission != "denied") {
            Notification.requestPermission(function (permission) {
              popNotice();
            });
        }
    };
} else {
    alert('瀏覽器不支持Notification');    
}
 
 
</script>  
 
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章