const createMessage = async data => {
  if (self.Notification.permission === 'granted') {
    const {title, body, ...rest} = data
      const n = new Notification(title, {
        body,
        data: rest
      })
      n.addEventListener('click', () => {
        n.close()
        self.postMessage({
          type: 'NotificationClicked',
          data: rest
        })
      })
      n.addEventListener('show', () => {
        self.postMessage({
          type: 'NotificationShowed',
          data: rest
        })
      })
      n.addEventListener('error', () => {
        self.postMessage({
          type: 'NotificationErrored',
          data: rest
        })
      })
      n.addEventListener('close', () => {
        self.postMessage({
          type: 'NotificationClosed',
          data: rest
        })
      })
   }
}

self.addEventListener('message', evt => {
  const {type, ...rest} = evt.data
  if (type === 'create') {
    createMessage(rest)
  }
})
