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.