0

我需要在窗口客户端隐藏时显示 SW 通知,并且在窗口客户端可见性状态可见时不应该显示通知。

我试过下面的代码。但 SW 显示默认通知。

本站已在后台更新

谁能帮帮我。

    self.addEventListener('push', function(event) {
    if (event.data) {
           console.log('This push event has data: ',event.data.text());
    } else {
           console.log('This push event has no data.');
    }

 // isClientFocused will return true if any of the window client is visible.

  const promiseChain = isClientFocused()
  .then(function(clientIsVisible){
    if (clientIsVisible) {
           return self.registration.showNotification('Dummy') 
     .then(function(){ 

      self.registration.getNotifications().then(function(notifications)
      {
         notifications.forEach(function(notification){
                notification.close();
            })
          })
      });
    }

//Client is not focused.So need to show notification.

    else{
        const title = 'POC Demo';
        const options = {
        body: event.data.text(),
        tag: 'data-notification',
        data: {
            message: event.data.text()
          }
       };

        return self.registration.showNotification(title,options); 
       }  
  });

  event.waitUntil(promiseChain);

 });
4

0 回答 0