Last active
August 28, 2019 14:41
-
-
Save sahajamit/4aa6b31af6c9d1bd42958bf521dc930c to your computer and use it in GitHub Desktop.
Code to listen to Web 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
//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