问题
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