Experiencing difficulties in detecting events other than install, activate, or push in my firebase-messaging-sw.js
. Notifications are being received and displayed, but I am unable to detect the event handler for notificationclick. When a firebase notification is clicked on, the notificationclick event does not show up in the service worker logs.
I am utilizing @nuxtjs/firebase 8.2.2 and have followed the setup instructions provided in the guide: .
In firebase-messaging-sw.js
self.addEventListener('notificationclick', function (event) {
console.log('Notification click');
event.notification.close();
const url = 'home';
event.waitUntil(
self.clients.matchAll({ type: 'window' }).then((windowClients) => {
// Check if there is already a window/tab open with the target URL
for (let i = 0; i < windowClients.length; i++) {
const client = windowClients[i];
// If so, just focus it.
if (client.url === url && 'focus' in client) {
console.log('focusing..');
return client.focus();
}
}
if (self.clients.openWindow) {
console.log('open window');
}
})
);
}, false);
I am also attempting to utilize postMessage()
to communicate with our service worker from a separate page to inject the user's email:
In account.vue
navigator.serviceWorker.ready.then((sw) => {
console.log('Requesting permission. Service worker: ', sw);
Notification.requestPermission().then(async (permission) => {
let token = null;
if (permission === 'granted') {
console.log('Notification permission granted.');
await sw.active.postMessage({ action: 'saveEmail', email: this.email });
}
});
});
In firebase-messaging-sw.js
self.addEventListener('message', async (msg) => {
console.log('message', msg);
const clients = await self.clients.matchAll({ type: 'window' });
for (const client of clients) {
client.postMessage(msg);
}
if (msg.data.action === 'saveEmail') {
USER_EMAIL = msg.data.email;
}
});
TLDR; Facing issues in monitoring notificationclick
events in my firebase-messaging-sw.js
.
EDIT: Added the push
event handler which is functional, however, still struggling to capture the notificationclick
event