对于发送桌面通知,我使用此代码。 (来自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.
}
当我按下按钮时,这将在同一页面上工作,但如果我想动态管理这些通知,该怎么办? 即如果我从后端(管理员)推送通知,它应该向前端显示消息(允许通知的用户)
答案 0 :(得分:2)
请按照以下步骤操作
您可以从会话中获取服务器端脚本中的用户ID,也可以从Ajax调用中获取参数。因此,根据用户ID,您将执行是否调用Ajax和 notifyMe()