You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Chrome : Version 121.0.6167.160 (Official Build) (64-bit)
Edge: Version 121.0.2277.106 (Official build) (64-bit)
Problem
We have a published a Chrome web addon (on Chrome Store) and we wanted to send Push notifications to users having installed on their browser.
So, to add this new feature:
On BO, I used the web-push library in our hosted NodeJS server (Express + MongoDB) to be able to subscribe all our users' addon instances (through FCM Vapid keys). The mongoDB persists all subscriptions JSON objects ({endpoint, keys})
On FO, i.e. on our web addon, we are relying on a registered ServiceWorker (which will act as a proxy between Push server and web addon) listening to "push" events from server.
In a normal scenario, all works like a charm and the notifications are gracefully poping on desktop.
Now, when I decide to deactivate my web addon (i.e. from Extensions manage page in Dev Mode), and my ServiceWorker remains registered, and I ignit a notification Push from server, I encounter errors on both sides :
on BO, this WebPushError saying that the 'push subscription has unsubscribed or expired
WebPushError: Received unexpected response code
at IncomingMessage.<anonymous> (/app/node_modules/web-push/src/web-push-lib.js:378:20)
at IncomingMessage.emit (node:events:530:35)
at endReadableNT (node:internal/streams/readable:1696:12)
at processTicksAndRejections (node:internal/process/task_queues:82:21) {
statusCode: 410,
headers: {
'content-type': 'text/plain; charset=utf-8',
'x-content-type-options': 'nosniff',
'x-frame-options': 'SAMEORIGIN',
'x-xss-protection': '0',
date: 'Thu, 08 Feb 2024 16:37:48 GMT',
'content-length': '47',
'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000'
},
body: 'push subscription has unsubscribed or expired.\n',
endpoint: 'https://fcm.googleapis.com/fcm/send/f7MxvCg_.....
on FO side, I get this error on "Application - push messaging" menu in devtools, saying Unsubscribed due to error - DELIVERY_PERMISSION_DENIED
So, from the push server point of view I understand the problem because the subscription has been unsubscribed by the PushManager associated with the ServiceWorker.
But I don't understand why this unsubscription happens ??
Is it because the ServiceWorker can't dispatch the push message to the deactivated addon, without a subscription's endpoint, and this causes the unsubscription due to this error ??
Expected
To have the push dispatched and have the notification, as usual when the web addon is activated.
Features Used
VAPID Support
Sending with Payload
Example / Reproduce Case
ServiceWorker.js (addon)
// Event listener for push event.self.addEventListener("push",(event)=>{console.debug("Push event",event);const{ title ="Test title", body ="Test message."}=event.data?.json();consticon="icons/icon_1024x1024.png";event.waitUntil(self?.registration?.showNotification(title,{
body,
icon,})// Show a notification with title and message);// Keep the service worker alive until the notification is created});self.addEventListener("pushsubscriptionchange",(event)=>{console.debug(event);});
background-script.js (addon)
exportconstregisterServiceWorker=async()=>{try{awaitnavigator?.serviceWorker.register("./serviceWorker.js");// Register a Service Worker}catch(error){console.error(`Service worker registration failed: ${error}`);}};exportconstsubscribeToNotification=async()=>{awaitnavigator?.serviceWorker?.ready.then(async(registration)=>{constsubscription=awaitregistration?.pushManager.subscribe({userVisibleOnly: true,applicationServerKey: urlBase64ToUint8Array(VAPID_PUBLIC_KEY),});if(subscription){constsubscriptionJson=subscription.toJSON();currentBrowser.runtime.setUninstallURL(`${SERVER_URL}/subscription/remove/${subscriptionJson.keys.auth}`,()=>console.debug("UninstallURL",`${SERVER_URL}/subscription/remove/${subscriptionJson.keys.auth}`));// Set uninstall URL to remove notification subscription on addon uninstall awaitfetchApi({url: `${SERVER_URL}/subscription`,reqInit: {body: JSON.stringify(subscription.toJSON()),method: "POST",headers: {"Content-Type": "application/json",},},});}registration.active.onstatechange=(event)=>console.info(`SW state change: ${event}`);// ServiceWorker state change eventregistration.active.onerror=(event)=>console.error(`SW error: ${event}`);// ServiceWorker error event}).catch((error)=>console.error(error));};
NOTE: Please test in a least two browsers (i.e. Chrome and Firefox). This
helps with diagnosing problems quicker.
Setup
Operating System: Linux (Ubuntu 22.04 LTS)
Node Version: 21.5.0
web-push Version: ^3.6.6
Chrome : Version 121.0.6167.160 (Official Build) (64-bit)
Edge: Version 121.0.2277.106 (Official build) (64-bit)
Problem
We have a published a Chrome web addon (on Chrome Store) and we wanted to send Push notifications to users having installed on their browser.
So, to add this new feature:
On BO, I used the web-push library in our hosted NodeJS server (Express + MongoDB) to be able to subscribe all our users' addon instances (through FCM Vapid keys). The mongoDB persists all subscriptions JSON objects ({endpoint, keys})
On FO, i.e. on our web addon, we are relying on a registered ServiceWorker (which will act as a proxy between Push server and web addon) listening to "push" events from server.
In a normal scenario, all works like a charm and the notifications are gracefully poping on desktop.
Now, when I decide to deactivate my web addon (i.e. from Extensions manage page in Dev Mode), and my ServiceWorker remains registered, and I ignit a notification Push from server, I encounter errors on both sides :
WebPushError
saying that the'push subscription has unsubscribed or expired
Unsubscribed due to error - DELIVERY_PERMISSION_DENIED
So, from the push server point of view I understand the problem because the subscription has been unsubscribed by the PushManager associated with the ServiceWorker.
But I don't understand why this unsubscription happens ??
Is it because the ServiceWorker can't dispatch the push message to the deactivated addon, without a subscription's endpoint, and this causes the unsubscription due to this error ??
Expected
To have the push dispatched and have the notification, as usual when the web addon is activated.
Features Used
Example / Reproduce Case
Other
I have tried to debug using either ServiceWorker registration
onerror
orpushsubscriptionchange
events, but in vain...The text was updated successfully, but these errors were encountered: