Created
August 29, 2019 15:14
-
-
Save Spyna/4b3e9632fdeb0362cb12a218503cc27f to your computer and use it in GitHub Desktop.
Full service worker to manage Push notifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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