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

前端 未结 4 1725
小蘑菇
小蘑菇 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:25

    Found a solution that worked for me right here.

    Just add this to your service worker:

    self.addEventListener('notificationclick', function(event) {
      console.log('On notification click: ', event.notification.tag);
      // Android doesn't close the notification when you click on it
      // See: http://crbug.com/463146
      event.notification.close();
    
      // This looks to see if the current is already open and
      // focuses if it is
      event.waitUntil(
        clients.matchAll({
          type: "window"
        })
        .then(function(clientList) {
          for (var i = 0; i < clientList.length; i++) {
            var client = clientList[i];
            if (client.url == '/' && 'focus' in client)
              return client.focus();
          }
          if (clients.openWindow) {
            return clients.openWindow('/');
          }
        })
      );
    });
    

提交回复
热议问题