ServiceWorker Chrome 推送通知 - 转发到新页面

ServiceWorker Chrome Push Notifications - Forward to new page

我想做的是查看我网站的选项卡是否在 Chrome 中打开,如果是,请关注它并将它们转发到我的新 URL穿过。

作为参考,当您看到 "event.notification.data" 时,它将是一个 link,例如“https://www.example.com/mobile/profile.php?id=Webmaster

我可以专注于该选项卡,但我无法将该选项卡重定向到我存储在 "event.notification.data"

中的 URL

这是我的代码

self.addEventListener('notificationclick', function(event) {
  event.notification.close();
  event.waitUntil(
    clients.matchAll({  
      type: "window" 
    })
    .then(function(clientList) {  
      // loop through all the tabs
      for (var i = 0; i < clientList.length; i++) {  
        var client = clientList[i]; 
        //check if a tab starts with the websites name 
        if (client.url.indexOf("https://www.example.com/mobile") == 0 && 'focus' in client){
          //a tab matched! Check if the data (a link) is there

          //------
          //THIS IS WHERE I NEED HELP
          //------
          if(event.notification.data != ''){
            //yes! event.notification.data is a link, focus on the tab and forward them there
            client.focus();
            client.navigate(event.notification.data);
          } else {
            //no! event.notification.data was blank, focus on the tab and forward them to the general site
            client.focus();
            client.navigate('https://www.example.com/mobile');
          } 

        }
      }  
      if (clients.openWindow) {
        if(event.notification.data != ''){
          clients.openWindow(event.notification.data);
        } else {
          clients.openWindow('https://www.heftynet.com/mobile');
        }  
      }
    })
  );
});

看起来 client.navigate() 尚未实现(截至 2015 年 9 月 10 日),甚至在 Chrome Canary 中也未实现。 It was specified only a few months ago, in June 2015. Here is the feature status page, and the relevant Chromium ticket。一旦 navigate() 实施,您的代码可能会工作。