Open mattyx97 opened 6 days ago
Hi, you can extend the service worker's script as described in docs. You can also refer to FCM docs.
// nuxt.config.ts
fcm: {
serviceWorkerScript: `
addEventListener("notificationclick", (event) => {
console.log("[firebase-messaging-sw.js] Notification clicked!", event)
});
messaging.onBackgroundMessage((payload) => {
console.log("[firebase-messaging-sw.js] Received background message ", payload);
})
`
},
This one
messaging.onBackgroundMessage((payload) => { console.log("[firebase-messaging-sw.js] Received background message ", payload); })
work as expected
Meanwhile this one is not working
addEventListener("notificationclick", (event) => { console.log("[firebase-messaging-sw.js] Notification clicked!", event) });
I can't reproduce the issue. However, it can be solved by registering the notificationclick
event listener before importing the Firebase scripts as they stop further listeners registration, https://github.com/firebase/quickstart-js/issues/102#issuecomment-278584863.
To confirm, you can add the notificationclick
listener below on runtime/server/routes/firebase-messaging-sw.get.ts
.
export default defineEventHandler((event) => {
// ....
return `
addEventListener("notificationclick", () => {}); // <--- ADD THIS
importScripts("https://www.gstatic.com/firebasejs/10.12.2/firebase-app-compat.js");
importScripts("https://www.gstatic.com/firebasejs/10.12.2/firebase-messaging-compat.js");
const firebaseConfig = ${firebaseConfigString};
const app = firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
${privateConfig.serviceWorkerScript?.trim() ?? ''}
`
})
Hi, could you help me to find how can i manage this action?
fcm: { firebaseConfig: { apiKey: "xxxxx", authDomain: "xxxx", projectId: "xxxx", storageBucket: "xxxxx", messagingSenderId: "xxxxxx", appId: "xxxxx", measurementId: "xxxx", }, vapidKey: "xxxxxxx", serviceWorkerScript: messaging.onBackgroundMessage((payload) => { const notificationTitle = payload.notification.title; const notificationOptions = { body: payload.notification.body, icon: payload.notification.icon || '/default-icon.png', tag: payload.notification.title, };
navigator.serviceWorker.getRegistration().then((registration) => { if (registration) { registration.update(); } }); }
console.log('notification clicked'); event.notification.close(); clients.openWindow("https://youtu.be/PAvHeRGZ_lA"); }); , },
why notificationclick doesn't work??