I want to implement push notifications to my website. I set up the service worker and the push api
navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
serviceWorkerRegistration.pushManager
.subscribe({
userVisibleOnly: true,
applicationServerKey: <vapidPublicKey>
});
});
And I can receive the subscription ID through
serviceWorkerRegistration.pushManager.getSubscription()
.then((subscription) => {
console.log(subscription);
});
If I want to host a custom server via VAPID and not use GCM (Google cloud messaging),
I'm supposed to send the subscription object to my push server then use
a webpush library to send the notification by supplying the subcription ID(s)
However, do the subscription IDs have an expiration, or does a subscription expire only when a user wants to unsubscribe?
I had the same issue and found that you can listen to the "pushsubscriptionchange" event which is fired when subscription expires. Subscribe again and register the new subscription in the server by sending a POST request with endpoint. Real world application would probably use also user identification.
Resource
Hope this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With