Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test pushsubscriptionchange event-handling code?

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?

like image 712
mjs Avatar asked Apr 13 '16 14:04

mjs


Video Answer


2 Answers

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"));
  });
}
like image 191
mjs Avatar answered Nov 03 '22 00:11

mjs


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.

pushsubscriptionchange event

like image 37
Marco Castelluccio Avatar answered Nov 03 '22 02:11

Marco Castelluccio