sw.js (785B)
1 self.addEventListener("install", function () { 2 self.skipWaiting(); 3 }); 4 5 self.addEventListener("activate", function (e) { 6 e.waitUntil(self.clients.claim()); 7 }); 8 9 self.addEventListener("push", async function (e) { 10 const clients = await self.clients.matchAll(); 11 let text = ""; 12 if (e.data) { 13 text = e.data.text(); 14 } 15 clients.forEach(function (client) { 16 client.postMessage({ type: "push", payload: text }); 17 }); 18 19 try { 20 const { title, body } = e.data.json(); 21 self.registration.showNotification(title, { body }); 22 } catch (e) {} 23 }); 24 25 self.addEventListener("pushsubscriptionchange", async function () { 26 const clients = await self.clients.matchAll(); 27 clients.forEach(function (client) { 28 client.postMessage({ type: "pushsubscriptionchange" }); 29 }); 30 });