[英]Dynamic desktop push notifications - PHP
For send desktop notifications I use this code. 对于发送桌面通知,我使用此代码。 (from https://developer.mozilla.org/en/docs/Web/API/notification ) (来自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.
}
This will work on same page when I press button, but what should I do if I want to manage these notifications dynamically ? 当我按下按钮时,这将在同一页面上工作,但如果我想动态管理这些通知,我该怎么办? ie If I push notification from backend (admin) it should show message to front-end (users' who allow notifications) 即如果我从后端(管理员)推送通知,它应该向前端显示消息(允许通知的用户)
Follow these steps 跟着这些步骤
You may get the user id in server side script either from session or a parameter from Ajax call. 您可以从会话或Ajax调用中的参数获取服务器端脚本中的用户标识。 So based on the user id you will perform whether to call or not to call Ajax and notifyMe() 因此,根据用户ID,您将执行是否调用Ajax和notifyMe()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.