Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpush notifications expiration

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?

like image 620
tosi Avatar asked Dec 01 '25 01:12

tosi


1 Answers

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.

like image 120
VoidA313 Avatar answered Dec 02 '25 14:12

VoidA313



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!