// Register event listener for the 'push' event.
self.addEventListener('push', function (event) {
    // Retrieve the textual payload from event.data (a PushMessageData object).
    // Other formats are supported (ArrayBuffer, Blob, JSON), check out the documentation
    // on https://developer.mozilla.org/en-US/docs/Web/API/PushMessageData.
    const eventInfo = event.data.text();
    const data = JSON.parse(eventInfo);
    const head = data.head || 'New Notification 🕺🕺';
    const body = data.body || 'This is default content. Your notification didn\'t have one 🙄🙄';
    const icon = data.img || 'static/img/svg/Logo.svg';

    let notificationOptions = {
        body: body,
        icon: icon,
      };
    if ('url' in data){
        notificationOptions['data'] = { url: data.url };
        notificationOptions['actions'] = [{action: "open_url", title: data.button_name}]
    }

    // Keep the service worker alive until the notification is created.
    event.waitUntil(
        self.registration.showNotification(head, notificationOptions)
    );
});

self.addEventListener('notificationclick', function(event) {

  switch(event.action){
    case 'open_url':
    clients.openWindow(event.notification.data.url);
    break;
    case 'any_other_action':
    clients.openWindow("https://www.example.com");
    break;
  }
}
, false);