Skip to content

Instantly share code, notes, and snippets.

@Spyna
Created August 26, 2019 13:27
Show Gist options
  • Save Spyna/e0f8eb405b039ffb400ea67792455d83 to your computer and use it in GitHub Desktop.
Save Spyna/e0f8eb405b039ffb400ea67792455d83 to your computer and use it in GitHub Desktop.
How to display web push notificaitons
/**
* 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
};
@jsinc-sebastian
Copy link

Good job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment