デスクトップ通知を送信するには、このコードを使用します。( https://developer.mozilla.org/en/docs/Web/API/notificationから)
<button onclick="notifyMe()">Notify me!</button>
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(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
// At last, if the user has denied notifications, and you
// want to be respectful there is no need to bother them any more.
}
これはボタンを押すと同じページで機能しますが、これらの通知を動的に管理したい場合はどうすればよいですか? つまり、バックエンド (管理者) から通知をプッシュすると、フロントエンド (通知を許可するユーザー) にメッセージが表示されます。