Skip to content

Instantly share code, notes, and snippets.

@Aybee5
Created July 17, 2020 14:08
Show Gist options
  • Save Aybee5/838c4aee24e0a1ff29df0673e383b6be to your computer and use it in GitHub Desktop.
Save Aybee5/838c4aee24e0a1ff29df0673e383b6be to your computer and use it in GitHub Desktop.
let dynamicCacheName = "dynamic",
showNotifications = (t) => {
self.registration.showNotification(t, {
body: "Reminder",
badge: "https://ibb.co/Lp2LyG3",
tag: Date.now().toString(36),
actions: [
{ action: "view", title: "View" },
{ action: "discard", title: "Discard" },
],
renotify: !0,
silent: !1,
vibrate: [250, 100, 200],
requireInteraction: !0,
});
};
const openApp = () => {
event.waitUntil(
(async function() {
const t = await clients.matchAll({ includeUncontrolled: !0 });
let e,
i = "https://remindo.netlify.app";
for (const n of t)
if (n.url.indexOf(i) >= 0) {
n.focus(), (e = n);
break;
}
e || (e = await clients.openWindow(i));
})()
);
};
self.addEventListener("install", (t) => {
console.log("sw installed successfully", t);
}),
self.addEventListener("activate", (t) => {
console.log("sw activated", t), self.skipWaiting();
}),
self.addEventListener("fetch", (t) => {
0 === t.request.url.indexOf("http") &&
t.respondWith(
caches
.open(dynamicCacheName)
.then((e) =>
e
.match(t.request)
.then(
(i) =>
i ||
fetch(t.request).then((i) => (e.put(t.request, i.clone()), i))
)
)
.catch((t) => {
console.log("sw error", t);
})
);
}),
self.addEventListener("notificationclick", (t) => {
switch ((t.notification.close(), t.action)) {
case "view":
default:
openApp();
}
}),
self.addEventListener("message", (t) => {
if (t.data.time && t.data.title) {
let e = t.data.time,
i = t.data.title;
setTimeout(showNotifications, e, i);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment