title: chrome service worker push notification show only when tab url of my site is closed or not active
body: <p>I would like to show GCM service worker message only when there is no open chrome tab in my website or the tab is open and not selected (active). </p>
  
  <p>Here is my code.</p>
  
  <p><strong>Error:</strong> chrome is not defined(…).</p>
  
  <p>manifest.json</p>
  
  <pre><code> {  
    "name": "Webplus Express",  
    "short_name": "Webplus Express",  
    "icons": [{  
      "src": "https://raw.githubusercontent.com/deanhume/typography/gh-pages/icons/typography.png",  
      "sizes": "192x192",
      "type": "image/png" 
      }],  
      "start_url": "/index.html",  
      "display": "standalone",  
      "gcm_sender_id": "298340340811",
      "gcm_user_visible_only": true,
      "background": {
      "scripts": ["service-worker.js"]
      },    
      "permissions": [
      "gcm","tabs","activeTab"
      ]  
    }
  </code></pre>
  
  <p>service-worker.js</p>
  
  <pre><code>    // The service worker running in background to receive the incoming
  // push notifications and user clicks
  self.addEventListener('push', function(event) {  
    // Since there is no payload data with the first version  
    // of push messages, we'll grab some data from  
    // an API and use it to populate a notification
    var s_id=0;  
    self.registration.pushManager.getSubscription().then(function(subscription) {
  
     if(subscription!=null){
  
     s_id= subscription.endpoint.split("/").slice(-1);
  
     var mURL="https://www.webplusexpress.com";
     var ok=true;
  
  
     chrome.tabs.getAllInWindow(undefined, function(tabs) {
      for (var i = 0;i&lt;tabs.length; i++) {
        if (tabs[i].url &amp;&amp; (tabs[i].url.indexOf(mURL)!=-1)) {
          //chrome.tabs.update(tab.id, {selected: true});
          if(tabs[i].highlighted){
            ok=false; 
          }  
          return;
        }
      }
  
    });
  
     if (ok){
     event.waitUntil(fetch('https://www.wpe.com/pushnotifyapi?s_id='+s_id).then(function(response) {  
  
        if (response.status !== 200) {  
          // Either show a message to the user explaining the error  
          // or enter a generic message and handle the
          // onnotificationclick event to direct the user to a web page  
          console.log('Looks like there was a problem. Status Code: ' + response.status);  
          throw new Error();  
        }
  
        // Examine the text in the response  
        return response.json().then(function(data) {  
          if (data.error || !data.notification) {  
            console.error('The API returned an error.', data.error);  
            throw new Error();  
          }  
  
          var title = data.notification.title;  
          var message = data.notification.message;  
          var icon = data.notification.icon;  
          console.log('icon received'+icon);
          var notificationTag = data.notification.tag;         
  
          return self.registration.showNotification(title, {  
            body: message,  
            icon: icon,  
            tag: notificationTag  
          });  
        });  
      }).catch(function(err) {
  
  
        console.error('Unable to retrieve data', err);
  
        var title = 'An error occurred';
        var message = 'We were unable to get the information for this push message';  
        var icon = 'https://www.webplusexpress.com/images/3web+carre.png';  
        var notificationTag = 'notification-error';  
        return self.registration.showNotification(title, {  
            body: message,  
            icon: icon,  
            tag: notificationTag  
          });  
      }) 
  
    ); 
    }
  
    }
  });
  
  });
  
  
  self.addEventListener('notificationclick', function(event) {  
    console.log('On notification click: ', event.notification.tag);  
    // Android doesn't close the notification when you click on it  
    // See: http://crbug.com/463146  
    event.notification.close();
  
    // This looks to see if the current is already open and  
    // focuses if it is  
    event.waitUntil(
      clients.matchAll({  
        type: "window"  
      })
      .then(function(clientList) {  
        for (var i = 0; i &lt; clientList.length; i++) {  
          var client = clientList[i];  
          if (client.url == '/' &amp;&amp; 'focus' in client)  
            return client.focus();  
        }  
        if (clients.openWindow) {
          return clients.openWindow('/');  
        }
      })
    );
  });
  </code></pre>