Overcomer
Overcomer

Reputation: 464

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?

Thanks.

Upvotes: 3

Views: 3958

Answers (2)

Matt Gaunt
Matt Gaunt

Reputation: 9821

You can send data with a message to a user but you need to encrypt the payload data. This has been in Firefox for a while in a few versions and is in a few versions of Chrome.

Check out libraries like the following:

Upvotes: 0

shershen
shershen

Reputation: 9993

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
            }));
          };
        });

Upvotes: 2

Related Questions