1. 程式人生 > >H5桌面訊息通知Notification

H5桌面訊息通知Notification

H5桌面訊息通知Notification

程式碼很簡單,直接複製即可看到效果

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>html5桌面通知</title>
    </head>
    <body>
        <input type="button" value="開啟桌面通知" onclick="showDeskTopNotice('通知','HTML5桌面訊息'
);"
>
<script> function showDeskTopNotice(title,msg){ var Notification = window.Notification || window.mozNotification || window.webkitNotification; if(Notification){ Notification.requestPermission(function(status){ //status預設值'default'等同於拒絕 'denied' 意味著使用者不想要通知 'granted' 意味著使用者同意啟用通知
if("granted" != status){ return; }else{ var tag = "sds"+Math.random(); var notify = new Notification( title, {
dir:'auto', lang:'zh-CN', tag:tag,//例項化的notification的id icon:'https://admin.hms.xin/group1/M00/00/01/rB_YCFsY0OeAX6Q8AAAQvnJ6aNc109.ico',//通知的縮圖,//icon 支援ico、png、jpg、jpeg格式 body:msg //通知的具體內容 } ); notify.onclick=function(){ //如果通知訊息被點選,通知視窗將被啟用 window.focus(); }, notify.onerror = function () { console.log("HTML5桌面訊息出錯!!!"); }; notify.onshow = function () { /*setTimeout(function(){ notify.close(); },2000)*/ }; notify.onclose = function () { console.log("HTML5桌面訊息關閉!!!"); }; } }); }else{ console.log("您的瀏覽器不支援桌面訊息"); } };
</script> </body> </html>