PWA notification how to redirect to installed app if it exists

195 views Asked by At

This seems fairly basic however I can't find an answer on here.

The MDN official docs show that getting a notification to navigate back to the app should look like this:

self.addEventListener("notificationclick", (event) => {
    event.notification.close();
  
    event.waitUntil(
      clients
        .matchAll({
          type: "navigate",
        })
        .then((clientList) => {
          for (const client of clientList) {
            if (client.url === "/" && "focus" in client) return client.focus();
          }
          if (clients.openWindow) return clients.openWindow("/");
        }),
    );
  });

It works fine in browser, but if I install my website as a PWA it simply redirects to the one opened in the browser (if it exists) or to a blank page otherwise.

Is there any way to get notifications to redirect either to their "origin" or, ideally, to the installed PWA.

Note: it's unclear to me if the web worker in this example is running on an isolated environment together with the installed PWA or still running in the main browser env -- which would seem relevant as to what it's client-list is.

0

There are 0 answers