简体   繁体   English

推送Web Notifications API

[英]Push with Web Notifications API

I've just started looking at the Web Notifications API. 我刚刚开始查看Web Notifications API。 This JS will immediately display a notification when I load the page: 这个JS会在我加载页面时立即显示通知:

if("Notification" in window) {
    if(Notification.permission == "granted") {
        var notification = new Notification("Notification Title", {"body":"Message Body", "icon":"my-icon.png"});
    } else {
        Notification.requestPermission(function (permission) {
            if (permission === "granted") {
                var notification = new Notification("Notification Title", {"body":"Message Body", "icon":"my-icon.png"});
            }
        });
    }
} else {
    alert("Your browser doesn't support notifications");
}

But what I am more interested in is having the notification displayed to me when someone else visits the same page. 但我更感兴趣的是当其他人访问同一页面时向我显示通知。 Whenever someone (not me) visits customer.php for example, the notification should show for me, not them. 每当有人(不是我)访问customer.php ,通知应该显示给我,而不是他们。

Can this be done with the Web Notification API and if so, how would I go about it? 是否可以使用Web Notification API完成此操作,如果是这样,我将如何处理它?

  1. Notification registration be for specific role (who want to receive the notification). 通知注册是针对特定角色(谁想要收到通知)。 so, your Notification.permission code be executed for specific users of application. 因此,您的Notification.permission代码将针对特定的应用程序用户执行。
  2. Add code to web site to push a message to your server, there could be multiple ways one possible, a JavaScript function getting called in the site load and in asynch manner post a message to server. 将代码添加到网站以将消息推送到您的服务器,可能有多种方式,在网站加载中调用JavaScript函数,并以异步方式将消息发送到服务器。
  3. Server side, you have an api receive the message publish the notification to the registered users ( under above point 1) 服务器端,你有一个api接收消息发布通知给注册用户(在上面的第1点)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM