Super simple and small Web Push and Notification service
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

32 lines
918 B

  1. // (A) INSTANT WORKER ACTIVATION
  2. self.addEventListener("install", evt => self.skipWaiting());
  3. // (B) CLAIM CONTROL INSTANTLY
  4. self.addEventListener("activate", evt => self.clients.claim());
  5. // (C) LISTEN TO PUSH
  6. self.addEventListener("push", evt => {
  7. const data = evt.data.json();
  8. console.log("got: " + evt.data);
  9. self.registration.showNotification(data.title, {
  10. body: data.body,
  11. icon: data.icon,
  12. image: data.image
  13. });
  14. });
  15. // (D) HANDLE USER INTERACTION
  16. // https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/notificationclick_event
  17. self.addEventListener( "notificationclick", (event) => {
  18. event.notification.close();
  19. if (event.action === "archive") {
  20. // User selected the Archive action.
  21. //archiveEmail();
  22. } else {
  23. // User selected (e.g., clicked in) the main body of notification.
  24. //clients.openWindow("/inbox");
  25. }
  26. },
  27. false,
  28. );