服务人员通知未在create-react-app中单击

时间:2019-07-11 12:38:10

标签: reactjs service-worker progressive-web-apps service-worker-events angular-pwa

我可以在哪里放置addEventListener(“ notificationclick”,()=> {})

我将以下代码放置在create-react-app中的serviceWorker.js中:

self.addEventListener("notificationclick", (event) => {
  event.waitUntil(async function () {
      const allClients = await clients.matchAll({
          includeUncontrolled: true
      });
      let chatClient;
      let appUrl = 'xyz';
      for (const client of allClients) {
      //here appUrl is the application url, we are checking it application tab is open
          if(client['url'].indexOf(appUrl) >= 0) 
          {
              client.focus();
              chatClient = client;
              break;
          }
      }
      if (!chatClient) {
          chatClient = await clients.openWindow(appUrl);
      }
  }());
});

上面的代码抛出未定义的错误客户端,并且当我单击通知时不会触发通知。

0 个答案:

没有答案