self.addEventListener("push", function (event) {
  const BASE_API_URL = "https://api.pushmarket.io";
  const data = JSON.parse(event.data.text());
    const headers = {
    "project-id": data.project_id
  };
  // sending impression to server tracker
  fetch(`${BASE_API_URL}/statistics/${data.id}/tracker?actionType=update-impression`, {headers: headers})

  event.waitUntil(
    self.registration.showNotification(data.title, {
      lang: data.lang,
      body: data.body,
      icon: data.icon,
      vibrate: [500, 100, 500],
    })
  );

  self.addEventListener("notificationclick", (event) => {
    event.notification.close();
    // sending click to server tracker
    fetch(`${BASE_API_URL}/statistics/${data.id}/tracker?actionType=update-click`, {headers: headers})
    if (data?.link) clients.openWindow(data?.link);
  });
});
