Overcomer Overcomer - 2 years ago 91
JSON Question

How to send user specific notification via Web push

I'm implementing web push on chrome and everything works well except that I don't know how to get user specific notification from the server. On send, everybody gets d same thing.

Is there a way I can pass endpoint ID to the latest notification request from service worker? Or how else can I do this?


Answer Source

There should be a data storage implemented of all subscribed clients on the server endpoint.txt that can be read and then message can be delivered to a specific user.

More on that part here: developer.mozilla.org/en/docs/Web/API/Push_API

Part of the function that sends broadcast message that you can modify to pick one client not all of them in for-loop: https://github.com/chrisdavidmills/push-api-demo/blob/gh-pages/server.js

  if(obj.statusType === 'chatMsg') {
        fs.readFile("endpoint.txt", function (err, buffer) {
          var string = buffer.toString();
          var array = string.split('\n');
          for(i = 0; i < (array.length-1); i++) {
            var subscriber = array[i].split(',');
            webPush.sendNotification(subscriber[2], 200, obj.key, JSON.stringify({
              action: 'chatMsg',
              name: obj.name,
              msg: obj.msg
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download