1

I've just started looking at the Web Notifications API. This JS will immediately display a notification when I load the page:

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.

Can this be done with the Web Notification API and if so, how would I go about it?

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
User_FTW
  • 504
  • 1
  • 16
  • 44
  • you need a webserver for that right? – ianace Apr 19 '17 at 02:13
  • You need some server code. Moreover you need to use the Push API (or a service like Pushpad) in order to deliver the notifications even if you're not online in that moment. First authenticate your users in some way (authentication or cookies), then subscribe them to push notifications (ask permission), then when someone visits that page, trigger a notification from your server. – collimarco Apr 19 '17 at 07:22

1 Answers1

0
  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.
  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.
  3. Server side, you have an api receive the message publish the notification to the registered users ( under above point 1)
dewraj singh
  • 128
  • 1
  • 10