Unsubscribed due to error DELIVERY_PERMISSION_DENIED

Aperta
#885 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
express, javascript, mongodb, node.js
Ambito
backend, web-dev

Direzione di ricerca

Start with the reported ServiceWorker.js, background-script.js, subscriptionRouter.js, and notificationRouter.js examples, then reproduce the deactivated-addon flow in Chrome and Edge using the listed versions. Trace the subscription lifecycle and the 410 response; done means establishing whether this is web-push behavior or an actionable library issue, with a reproducible case or documented explanation.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
  • Other

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

Screenshot from 2024-02-09 16-42-32

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();
  const icon = "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)
export const registerServiceWorker = async () => {
  try {
    await navigator?.serviceWorker.register("./serviceWorker.js"); // Register a Service Worker
  } catch (error) {
    console.error(`Service worker registration failed: ${error}`);
  }
};

export const subscribeToNotification = async () => {
  await navigator?.serviceWorker?.ready
    .then(async (registration) => {
      const subscription = await registration?.pushManager.subscribe({
        userVisibleOnly: true,
        applicationServerKey: urlBase64ToUint8Array(VAPID_PUBLIC_KEY),
      });
      if (subscription) {
        const subscriptionJson = 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 
        await fetchApi({
          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 event
      registration.active.onerror = (event) =>
        console.error(`SW error: ${event}`); // ServiceWorker error event
    })
    .catch((error) => console.error(error));
};
  • subscriptionRouter.js (server)
  subscriptionRouter
  .post('/subscription', async (req, res) => {
    try {
      await (await db())
        ?.collection(`${SUBSCRIPTION_DB_COLL_NAME}`)
        ?.insertOne(req.body);
      res.status(201).json({ message: 'Subscription created' });
    } catch (error) {
      console.error(error);
      res.json({ message: 'Subscription creation error', error });
    }
  })
  • notificationRouter.js (server)
notificationRouter
 .get('/notification/push', async (req, res) => {
    try {
      const notifications =
        (await (await db())
          ?.collection(`${NOTIFICATION_DB_COLL_NAME}`)
          ?.find({})
          .toArray()) ?? [];
      const subscriptions = await getSubscriptions();
      if (notifications?.length > 0 && subscriptions?.length > 0) {
        subscriptions?.map(async ({ endpoint, keys }) => {
          console.debug({
            endpoint,
            keys,
          });
          await webPush
            .sendNotification(
              { endpoint, keys },
              JSON.stringify(notifications.pop()),
              pushOptions
            )
            .then((response) => {
              console.debug(response);
              res.json({
                message: `Notification Push done for ${subscriptions?.length} subscriptions`,
              });
            })
            .catch((error) => {
              console.error(error);
              if (error?.statusCode === 410) deleteSubscription(keys?.auth);
            });
        });
      } else
        res.json({
          message: `Notification Push not done because no notification exists`,
        });
    } catch (error) {
      console.error(error);
      res.json({ message: `Notification push error`, error });
    }
  });

Other

I have tried to debug using either ServiceWorker registration onerror or pushsubscriptionchange events, but in vain...

Lingua principale
JavaScript
Stelle
3.6k
Fork
328
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di web-push-libs/web-push

Tutte le issue di web-push-libs/web-push

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.