I have set up desktop notifications using:
Notification.requestPermission((result) => {
if (result === "granted") {
navigator.serviceWorker.register("/dummy.js").then((registration) => {
registration.showNotification("Hello", {
body: "Buzz! Buzz!",
actions: [{...}],
});
});
}
});
I can't use the Notification
constructor, as I need to show buttons (actions
) on the notifications, and for some reason the constructor doesn't support this.
I have no intention of firing or listening to the these notifications when the app is not open in a browser tab.
Is there a way to listen to the "notificationclick"
and other notification related events from the main browsing context, and not from within the worker?
Alternatively is there any way to use the full set of features on the Notification
constructor without having a worker?
To clarify further: I don't need the worker for anything other than using actions
on the notifications. I only need to fire notifications / listen to notifications while the app is running in the browser, so I want to interact with the worker as little as possible. Writing logic for notification events in the worker itself is highly undesirable.