【问题标题】:ServiceWorker Chrome Push Notifications - Forward to new pageServiceWorker Chrome 推送通知 - 转发到新页面
【发布时间】:2015-12-06 21:56:45
【问题描述】:

我要做的是查看我网站的标签是否在 Chrome 中打开,如果是,请关注它并将它们转发到我正在通过的新 URL。

作为参考,当您看到“event.notification.data”时,它将是一个链接,例如“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');
        }  
      }
    })
  );
});

【问题讨论】:

    标签: google-chrome notifications service-worker web-push push-api


    【解决方案1】:

    看起来client.navigate() 尚未实现(截至 2015 年 9 月 10 日),甚至在 Chrome Canary 中也没有实现。 It was specified only a few months ago,2015 年 6 月。这里是 feature status page,以及相关的 Chromium ticket。一旦实现navigate(),您的代码可能会工作。

    【讨论】:

    • 我读了很多,但我想我不想相信它。感谢您的帮助。
    • 你也可以进入inspector自己验证。我通过获取客户端对象在 Canary 中检查了这一点,并在其 __proto__ 属性上看到它有一个 focus() 方法但没有 navigate() 方法。
    猜你喜欢
    • 2017-02-23
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多