Skip to content Skip to sidebar Skip to footer

Service Worker: Redirecting A User To Different Url When He Clicks The Notification

This snippet helps in either bring in the app tab in focus or open a new tab with that URL. If the tab is already open, is there a way to change the URL (based on the notification

Solution 1:

The WindowClient.navigate() method sounds like what you want. You should be able to do something like:

// Assume that you want to find the first window that is currently open
// to originalUrl, and navigate that window to navigationUrl.
// If there is no window currently at originalUrl, then open a new window.
event.waitUntil(
  clients.matchAll({type: 'window'})
    .then(clients => clients.filter(client => client.url === originalUrl))
    .then(matchingClients => {
      if (matchingClients[0]) {
        return matchingClients[0].navigate(navigationUrl)
                 .then(client => client.focus());
      }

      return clients.openWindow(navigationUrl);
    })
);

Post a Comment for "Service Worker: Redirecting A User To Different Url When He Clicks The Notification"