2017-07-07 34 views
0

我有一個網絡工作者,我添加了'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

回答

-1

我解決了使用下面的代碼的通知單擊事件:

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