<!-- 手動執行 -->
<button onclick="notifyMe()">Notify me!</button>
<script>
Notification.requestPermission(function(status){
console.log('User Choice', status);
if (status !== 'granted') {
console.log('推播允許被拒絕了!');
}
});
function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
}
// 每 2 秒執行一次
setInterval(notifyMe, 2000);
</script>