How can I initiate a PWA (progressive webapp) open from a click on a push notification?

前端 未结 4 1812
小蘑菇
小蘑菇 2021-02-04 09:56

Following this example, I see how PWA can open urls but how can I use push notification to launch the app itself? Not in the browser but the full screen version PWA.

4条回答
  •  忘掉有多难
    2021-02-04 10:37

    Taken from Jake Archibald's emojoy demo:

    self.addEventListener('notificationclick', event => {
        const rootUrl = new URL('/', location).href;
        event.notification.close();
        // Enumerate windows, and call window.focus(), or open a new one.
        event.waitUntil(
          clients.matchAll().then(matchedClients => {
            for (let client of matchedClients) {
              if (client.url === rootUrl) {
                return client.focus();
              }
            }
            return clients.openWindow("/");
          })
        );
    });
    

提交回复
热议问题