调用浏览器系统通知
语法概述
通过浏览器的Notification实现,需要发送通知时调用showNotice(msg)方法即可。
需要注意的是,chrome只允许https的站点调用Notification,如果是http站点会默认拒绝,并且无法修改。但 http://localhost/ ,这个url被特批可调用
调用
function showNotice(msg) {
//发送通知
newNotify = function () {
var notification = new Notification("系统通知:", {
dir: "auto",
lang: "hi",
requireInteraction: true,
//tag: "testTag",
icon: "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top_86d58ae1.png",
body: msg
});
notification.onclick = function (event) {
//回到发送此通知的页面
window.focus();
//回来后要做什么
console.log("I'm back");
}
}
//权限判断
if (Notification.permission == "granted") {
newNotify();
} else {
//请求权限
Notification.requestPermission(function (perm) {
if (perm == "granted") {
newNotify();
}
})
}