How can I test pushsubscriptionchange event-handling code?

限于喜欢 提交于 2019-11-30 05:17:39

问题


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?


回答1:


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"));
  });
}



回答2:


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.



来源:https://stackoverflow.com/questions/36602136/how-can-i-test-pushsubscriptionchange-event-handling-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!