Created
August 26, 2019 13:27
-
-
Save Spyna/e0f8eb405b039ffb400ea67792455d83 to your computer and use it in GitHub Desktop.
How to display web push notificaitons
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
/** | |
* checks if Push notification and service workers are supported by your browser | |
*/ | |
function isPushNotificationSupported() { | |
return "serviceWorker" in navigator && "PushManager" in window; | |
} | |
/** | |
* asks user consent to receive push notifications and returns the response of the user, one of granted, default, denied | |
*/ | |
function initializePushNotifications() { | |
// request user grant to show notification | |
return Notification.requestPermission(function(result) { | |
return result; | |
}); | |
} | |
/** | |
* shows a notification | |
*/ | |
function sendNotification() { | |
const img = "/images/jason-leung-HM6TMmevbZQ-unsplash.jpg"; | |
const text = "Take a look at this brand new t-shirt!"; | |
const title = "New Product Available"; | |
const options = { | |
body: text, | |
icon: "/images/jason-leung-HM6TMmevbZQ-unsplash.jpg", | |
vibrate: [200, 100, 200], | |
tag: "new-product", | |
image: img, | |
badge: "https://spyna.it/icons/android-icon-192x192.png", | |
actions: [{ action: "Detail", title: "View", icon: "https://via.placeholder.com/128/ff0000" }] | |
}; | |
navigator.serviceWorker.ready.then(function(serviceWorker) { | |
serviceWorker.showNotification(title, options); | |
}); | |
} | |
/** | |
* | |
*/ | |
function registerServiceWorker() { | |
navigator.serviceWorker.register("/sw.js").then(function(swRegistration) { | |
//you can do something with the service wrker registration (swRegistration) | |
}); | |
} | |
export { | |
isPushNotificationSupported, | |
initializePushNotifications, | |
registerServiceWorker, | |
sendNotification | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good job