Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending PN to specific user(s) #3

Open
thevalo opened this issue Oct 13, 2020 · 1 comment
Open

Sending PN to specific user(s) #3

thevalo opened this issue Oct 13, 2020 · 1 comment

Comments

@thevalo
Copy link

thevalo commented Oct 13, 2020

Hello, thank you for this awesome module (if I can call it so).

I've set up it successfully and can receive notifications, however, what I'm not figuring out is that how to target a specific user (or users) to send them a notification?

Presumably, I think I'd modify the table to include a unique ID column, but how can I call the desired functions to send PNs WHERE unique ID matches my needs?

Thank you.

@Stefanius67
Copy link
Owner

Hi there,
sorry that I am only now writing - but I was heavily involved in other areas and could only now devote myself to the problem described.

To be able to send notifications to one or more selected users, three extensions or steps are required:

  1. In order for a user ID to be saved at all, it must first be transferred from the client side when subscribing (only the client itself can 'know who he is') - I will explain this problem in more detail below!
  2. As you have already written, the table (and thus the data provider) in which the subscriptions are stored must of course be supplemented with an additional data field for the user ID to assign this subscription to an specific user. The ID will be passed in first step in the same way, the userAgent is passed so far.

a new data provider class based on one of the two contained in the package should be created and used.

  1. In order to finally send a notification to one or more selected users, the corresponding data records must be selected accordingly and transferred to the PNServer class.

For this purpose, either a new class can be created that extends PNServer and implements an additional loadSubscriptionsEx($userID) method or the desired subscription(s) can be transferred to a PNServer object in a loop using addSubscription()
If only a single notification is to be sent, the (new!) method pushSingle() from PNServer can also be used directly.

Extending the client script to transfer an (existing) user ID.

Since the service worker runs in a separate thread in the browser that does not have access to the currently active page (or its DOM), the process of subscribing has to be slightly modified in order to pass the user ID (or any further data) to the service worker before it subscribes to the push notifications.

In the current code example, the service worker subscribes to the push notifications immediately upon activation and the returned information is then sent directly to the server for storage. This means that it is not possible to pass additional data that can be sent to the server for storage together with the subscription information.
To pass data before subscribing, this call at last must be made later:
Calling the subscription is moved from the handler for the 'activate' event into the handler of the 'message' event. This event have to be triggered manuall by the client by calling postMessage(...), whereby it is possible to pass any data object. In the handler for this event, the passed data have to be saved locally so that it can be transferred to the server for storage after the subscription has been made.

Since these changes are a bit more complex, I have them implemented in the following sample scripts:

  • PNClientEx.js
  • PNServiceWorkerEx.js
  • PNTestClientEx.html

... it should be noted that these are sample scripts that I didn't have too much time to test for ...

The server-side changes/extensions should be feasible according to the comments above ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants