Last active
August 26, 2019 15:39
-
-
Save Spyna/5eb3f092c867074fdf8b3b967a8a8531 to your computer and use it in GitHub Desktop.
How to receive a web push notification i a service worker
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 notificationText = event.data.text(); | |
const title = "A brand new notification!" | |
const options = { | |
//data: url, | |
data: "something you want to send within the notification, such an URL to open" | |
//body: text, | |
body: notificationText | |
//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" }] | |
}; | |
//call the method showNotification to show the notification | |
event.waitUntil(self.registration.showNotification(title, options)); | |
} | |
self.addEventListener("push", receivePushNotification); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment