summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/androidTest/assets/www/push/sw.js
blob: 2e51383205f967b472fdddf77835db252634cf3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
self.addEventListener("install", function () {
  self.skipWaiting();
});

self.addEventListener("activate", function (e) {
  e.waitUntil(self.clients.claim());
});

self.addEventListener("push", async function (e) {
  const clients = await self.clients.matchAll();
  let text = "";
  if (e.data) {
    text = e.data.text();
  }
  clients.forEach(function (client) {
    client.postMessage({ type: "push", payload: text });
  });

  try {
    const { title, body } = e.data.json();
    self.registration.showNotification(title, { body });
  } catch (e) {}
});

self.addEventListener("pushsubscriptionchange", async function (e) {
  const clients = await self.clients.matchAll();
  clients.forEach(function (client) {
    client.postMessage({ type: "pushsubscriptionchange" });
  });
});