桌面通知 notification 示例
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<script>
function notifyMe() {
// 先檢查瀏覽器是否支持
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!");
}
// 否則我們需要向用戶獲取權(quán)限
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function(permission) {
// 如果用戶同意山橄,就可以向他們發(fā)送通知
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
// 最后批什,如果用戶已經(jīng)授權(quán)了相關(guān)通知
// 你出于尊重畅卓,就不需要再打擾他們了
}
</script>
</head>
<body>
<button onclick="notifyMe()">Notify me!</button>
</body>
</html>