The pushsubscriptionchange
event is fired by the browser when the push notification server wants the client to resubscribe. How can I manually trigger this event for testing?
Asked
Active
Viewed 3,758 times
8
2 Answers
5
Unfortunately it's not possible to do any end-to-end testing of this feature. The best you can do is fire the event via JS from the service worker:
function triggerSubscriptionChange() {
registration.pushManager.getSubscription().then(subscription => {
if (subscription) {
console.log("subscribed, subscription", subscription);
return subscription.unsubscribe();
}
}).then(() => {
console.log("unsubscribed");
return registration.pushManager.subscribe({
userVisibleOnly: true
});
}).then(subscription => {
console.log("subscribed, subscription", subscription);
self.dispatchEvent(new ExtendableEvent("pushsubscriptionchange"));
});
}

mjs
- 63,493
- 27
- 91
- 122
-
You are not actually testing a subscription *refresh* with your method, because your event lacks `event.oldSubscription` and `event.newSubscription`. It would be useful to have a button in the browser dev tools to fire a real `pushsubscriptionchange`: see this [feature proposal in the Chromium bug tracker](https://bugs.chromium.org/p/chromium/issues/detail?id=753163) – collimarco Sep 04 '18 at 12:14
-
Also, using your method I get this error: `Failed to execute 'waitUntil' on 'ExtendableEvent': Can not call waitUntil on a script constructed ExtendableEvent.` – collimarco Sep 04 '18 at 16:15
-
@collimarco How to then update subscription in case of chrome?? Should we allow it to automatically invalidate itself? And after that check if subscription exists using getSubscription() if it is null then request for permission. Is this flow correct? – rock stone Jun 22 '22 at 18:55
5
The event is also triggered when the user removes and re-grants the push permission (see https://github.com/w3c/push-api/issues/116).
For example, in Firefox you can click on the site identity icon, in the "Permission" section, select "Block" for "Receive Notifications", then select "Allow".
A pushsubscriptionchange
event will be triggered.

Marco Castelluccio
- 10,152
- 2
- 33
- 48
-
3Tried this and the event didn't trigger ... Also I see https://github.com/w3c/push-api/issues/116 open – Neeraj Krishna Nov 24 '16 at 07:05
-
It used to work, as you can see from my screenshot. I don't know if it stopped working since then. You could also try removing the website from the list of websites allowed to send notifications in "Preferences"->"Content". – Marco Castelluccio Nov 25 '16 at 10:05
-
[Last comment](https://github.com/w3c/push-api/issues/116#issuecomment-280743159) in this issue says _it's now defined that user agents MAY fire the pushsubscriptionchange event when permission is revoked_ – piotr_cz Jul 15 '17 at 15:45
-
I managed to fire the event in firefox 57 by clearing the permission, then calling `registration.pushManager.subscribe()` and then allowing the permission again. – Jespertheend Jan 15 '18 at 00:34
-
I was able to trigger it just as Marco suggested, it's just not as easy in current Firefox (v66) because now you have to go to settings, find the website and disable/enable form there. – Igor Jerosimić Apr 25 '19 at 09:23
-
Seems like `pushsubscriptionchange event` only implemented by firefox. It wasn't supported by major browser such as chrome. – Song Lim Feb 21 '23 at 09:30