一、什么是 Notification
Notification
是浏览器最小化后在桌面显示消息的一种方法- 类似于
360
等流氓软件在桌面右下角的弹窗广告 - 它与浏览器是脱离的,消息是置顶的

Notification
是浏览器最小化后在桌面显示消息的一种方法360
等流氓软件在桌面右下角的弹窗广告Notification.permission
的值来查看你是否已经有权限Notification.requestPermission()
可以询问用户是否允许通知Notification.requestPermission()
new Notification($title, $options)
使用通知推送功能new Notification("温馨提醒", { body: "飞兔小哥送你一份奖品待领取", icon: "https://autofelix.github.io/autofelix/u/favicon.ico", data: "https://autofelix.blog.csdn.net/" });
"Notification" in window
方法去检测function notify() { // 先检查浏览器是否支持 if (!("Notification" in window)) { alert("This browser does not support desktop notification"); } // 检查用户是否同意接受通知 else if (Notification.permission === "granted") { // If it's okay let's create a notification var notification = new Notification("Hi there!"); } // 否则我们需要向用户获取权限 else if (Notification.permission !== "denied") { Notification.requestPermission().then(function (permission) { // 如果用户接受权限,我们就可以发起一条消息 if (permission === "granted") { var notification = new Notification("Hi there!"); } }); } // 最后,如果执行到这里,说明用户已经拒绝对相关通知进行授权 // 出于尊重,我们不应该再打扰他们了 }
var notification = new Notification("温馨提醒", { body: "飞兔小哥送你一份奖品待领取", icon: "https://autofelix.github.io/autofelix/u/favicon.ico", data: "https://autofelix.blog.csdn.net/" }); notification.onshow = function (event) { console.log("show : ", event); }; notification.onclose = function (event) { console.log("close : ", event); }; notification.onclick = function (event) { console.log("click : ", event); // 当点击事件触发,打开指定的url window.open(event.target.data) notification.close(); }; notification.onerror = function (event) { console.log("close : ", event); };
var notification = new Notification('标题'); notification.onshow = function () { setTimeout(function () { notification.close(); }, 3000); }
评论已关闭。