I'm looking for a way to handle onclick
event on a Notification, when the app is in foreground and tab is in focus.
I have this:
self.addEventListener('notificationclick', function(event) {....
in my service worker file firebase-messaging-sw.js
, but this only gets triggered when the app is in the background or not in focus. I think this is how it's supposed to work.
Earlier I was doing this:
const options = {
body: notification.body,
click_action: notification.data.click_action,
data: notification.data,
tag: tag
};
const notif = new Notification(notification.title, options);
notif.onclick = function() {
alert('sdfsdf'); // working FINE
};
However, new Notification
doesn't seem to be working at all in Android based browsers like chrome, brave, firefox, etc.
Now I'm using this:
navigator.serviceWorker.getRegistrations().then(function() { ....
to find my service worker registration (ServiceWorkerRegistration
interface) and call showNotification
method on it, which is working fine cross-platform.
But how do I bind the onclick
on Notification
that showNotification
generates
I tried doing this:
// v = ServiceWorkerRegistration
v.getNotifications({ tag: tag })
.then(function(notificationsList) {
if( notif = notificationsList[0] ) {
console.log("notif: ", notif);
notif.onclick = function() {
alert('new notif');
};
}
});
the onclick
gets bind, but still doesn't work.
I might be missing something here.