Skip to content

Instantly share code, notes, and snippets.

@Spyna
Created August 29, 2019 15:14
Show Gist options
  • Save Spyna/4b3e9632fdeb0362cb12a218503cc27f to your computer and use it in GitHub Desktop.
Save Spyna/4b3e9632fdeb0362cb12a218503cc27f to your computer and use it in GitHub Desktop.
Full service worker to manage Push notifications
function receivePushNotification(event) {
console.log("[Service Worker] Push Received.");
const { image, tag, url, title, text } = event.data.json();
const options = {
data: url,
body: text,
icon: image,
vibrate: [200, 100, 200],
tag: tag,
image: image,
badge: "https://spyna.it/icons/favicon.ico",
actions: [{ action: "Detail", title: "View", icon: "https://via.placeholder.com/128/ff0000" }]
};
event.waitUntil(self.registration.showNotification(title, options));
}
function openPushNotification(event) {
console.log("[Service Worker] Notification click Received.", event.notification.data);
event.notification.close();
event.waitUntil(clients.openWindow(event.notification.data));
}
self.addEventListener("push", receivePushNotification);
self.addEventListener("notificationclick", openPushNotification);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment