var push_target_link = '';
self.addEventListener('push', function (event) {
    if (!(self.Notification && self.Notification.permission === 'granted')) {
        return;
    }

    const sendNotification = body => {
        // you could refresh a notification badge here with postMessage API
        const title = "YES4Trade Push Info";
        return self.registration.showNotification(title, {
            body,
        });
    };

    if (event.data) {
	msg = event.data.text();
	var MSG_OBJ = JSON.parse(msg);
        const message = MSG_OBJ.message;
	push_target_link = MSG_OBJ.push_target_link;
        event.waitUntil(sendNotification(message));
    }
});

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 < clientList.length; i++) {  
        var client = clientList[i];  
        if (client.url == '/' && 'focus' in client)  
          return client.focus();  
      }  
      if (clients.openWindow) {
        return clients.openWindow(push_target_link);  
      }
    })
  );
});

