Notification.requestPermission(function (status) {
console.log(status) // 仅当值为 "granted" 时显示通知
var n = new Notification('title', { body: 'notification body' }) // 显示通知
})
<!--
* @Description:
* @Author: huhaonan@sics.ac.cn
* @Date: 2021-07-13 17:37:54
* @LastEditors: huhaonan@sics.ac.cn
* @LastEditTime: 2022-06-23 14:52:45
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=header, initial-scale=1.0">
<title>Notification</title>
</head>
<body>
<header>
</header>
<script type="text/javascript">
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 = {}) {
console.log('lllllll')
if (!("Notification" in window)) {
return console.error("This browser does 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();
}
})
</script>
</body>
</html>