Skip to content

Instantly share code, notes, and snippets.

@sahajamit
Last active August 28, 2019 14:41
Show Gist options
  • Save sahajamit/4aa6b31af6c9d1bd42958bf521dc930c to your computer and use it in GitHub Desktop.
Save sahajamit/4aa6b31af6c9d1bd42958bf521dc930c to your computer and use it in GitHub Desktop.
Code to listen to Web Push Notifications
//inject before starting the notification testing
window.notifications = [];
window.DefaultNotification = window.Notification;
(function () {
function notificationCallback(title, opt) {
console.log("notification title: ", title);
console.log("notification body: ", opt.body);
console.log("notification tag: ", opt.tag);
console.log("notification icon: ", opt.icon);
}
const handler = {
construct(target, args) {
notificationCallback(...args);
var notification = new target(...args);
notifications.push(notification);
return notification;
}
};
const ProxifiedNotification = new Proxy(Notification, handler);
window.Notification = ProxifiedNotification;
})();
//resetting back to the default behaviour
window.Notification = window.DefaultNotification;
//For service Worker:
var registration = await navigator.serviceWorker.getRegistration("https://framework.realtime.co/demo/web-push/")
var notifications = await registration.getNotifications();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment