Web服务工作者通知点击 - 在同一页上打开网址

Web服务工作者通知点击 - 在同一页上打开网址

问题描述:

我有一个网络工作者,我添加了'notificationclick'事件。 我想导航到相同选项卡中的网址,而不是打开新选项卡。Web服务工作者通知点击 - 在同一页上打开网址

任何线索如何做到这一点?

self.addEventListener('notificationclick', function (event) { 
    const id = event.notification.data.id; 
    let path = 'http://' + event.target.location.host; 
    if (event.notification.data.type === 'invite') 
     path += '/invite/' + id; 
    else 
     path += '/notification/' + id; 
    event.notification.close(); 
    const promiseChain = clients.matchAll({ 
     type: 'window', 
     includeUncontrolled: true 
    }) 
     .then((windowClients) => { 
      let matchingClient = null; 
      for (let i = 0; i < windowClients.length; i++) { 
       const windowClient = windowClients[i]; 
       if (windowClient.url === path) { 
        matchingClient = windowClient; 
        break; 
       } 
      } 

      if (matchingClient) { 
       return matchingClient.focus(); 
      } else { 
       return clients.openWindow(path); 
      } 
     }); 

    event.waitUntil(promiseChain); 
}); 
+0

你解决这个问题? –

+0

没有。在互联网上没有关于它:( – MusicGindos

我解决了使用下面的代码的通知单击事件:

window.open("http://url.here","_self");