HTML5桌面通知提醒功能的实现-
发布时间:08/01 来源:未知 浏览:
关键词:
概括代码如下所示:
;(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()是个要求 ,让阅读器涌现可否允许通知的提醒 和 Notification.permission 同样 返回 "3"个值 "granted" "default" "denied" 户允许通知, 会员当前尚无管, 会员厌弃。