Closed tillmon closed 5 years ago
On the back-end side, sure. On the front end side, maybe some adjustments would be required in the JS to be sure of that. Have you tried it yet?
Yes I tried it. I implemented a user change function which updates the active subscription.
https://github.com/tillmon/webpush-bundle
Another way would be to create a new subscription but then you need a separate service worker.
What do you think?
Theorically you shouldn't need a separate service worker. The service worker is just here to show a notification and process actions like clicks, etc, it doesn't handle subscriptions on the domain side.
You don't need to do changes on the back-end side either: a single JS subscription object can be associated to 1 or more users in the persistence layer.
You just need to fire a POST request to /webpush
(or call webpush.subscribe()
if you use the JS example) as soon as the user logs in.
But you also need to make sure that the subscription of the previous logged in users is unsubscribed otherwise the user gets notifications which ware not intended for him.
I could not find a simple solution to track user log in/out in JS and use webpush.subscribe()
or webpush.unsubscribe()
.
@tillmon the main advantage of Webpush Notifications is that they work even if the browser is closed / the device is offline, so, regardless of whether the user is logged in or not. If you need to display notifications to the current user logged in, I'm sorry that this library may not be the best option.
Is it possible to set up subscriptions for different users on the same browser (i.e. PC)?