Mark
Mark

Reputation: 615

How to get old subscription data when using pushsubscriptionchange

I am saving subscription endpoint, auth and p256dh keys in a db, and I wanted to implement pushsubscriptionchange event on my service worker to resubscribe and update the old subscription I have in the db when it expires, but I can't find a way to get the old subscription data. I am using this simple code to test it.

self.addEventListener('pushsubscriptionchange', function(event) {
    console.log('subscription expired');
    event.waitUntil(
        self.registration.pushManager.subscribe({ userVisibleOnly: true })
        .then(function(subscription) {
            console.log('subscribed again');
            // send subscription data to server to update database
        })
    );
});

To trigger the event in firefox I remove the permissions and grant them again, the event fires fine but I can't find any information that would let me know the old subscription data so I can delete/update it on the db, so I will end up with expired subscriptions plus the new ones.

How can I get the old subscription data?

Forgot to add, I have no other way to identify the user, they are not registered and it's not an app, just a website.

Upvotes: 1

Views: 700

Answers (2)

velop
velop

Reputation: 3224

This might have changed since the original answer. At least the documentation of mozilla states that there is event.oldSubscription on the event object.

https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/pushsubscriptionchange_event

Upvotes: 0

Serginho
Serginho

Reputation: 7490

You can't. Any browser pass params in the pushsubscriptionchange currently.

For more details see this: How to handle WebPush API PushSubscriptionChange event in modern browsers

Upvotes: 2

Related Questions