跳到主要内容

桌面通知

方案A

:::details 点击查看代码

function doNotify(title, options = {}, events = {}) {
const notification = new Notification(title, options);
for (let event in events) {
notification[event] = events[event];
}
}

function notify(title, options = {}, events = {}) {
if (!("Notification" in window)) {
return console.error(
"The brower dones not support desktop notification"
);
} else if (Notification.permission === "granted") {
doNotify(title, options, events);
} else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
if (permission == "granted") {
doNotify(title, options, events);
}
});
}
}

notify(
"温馨提示",
{
icon: "https://sf1-ttcdn-tos.pstatp.com/img/user-avatar/f1a9f122e925aeef5e4534ff7f706729~300x300.image",
body: "我已经爱上你!渴望能在一起!",
tag: "prize",
},
{
onclick(ev) {
console.log(ev);
ev.target.close();
window.focus();
},
}
);

:::