File: /var/www/nclive/firebase-messaging-sw.js
importScripts("https://www.gstatic.com/firebasejs/11.0.1/firebase-app-compat.js");
importScripts("https://www.gstatic.com/firebasejs/11.0.1/firebase-messaging-compat.js");
firebase.initializeApp({
apiKey: "AIzaSyB7mQGdLNFy2UTH93p3A7c5m-R244_4amg",
projectId: "naukriconnectapp-45431",
messagingSenderId: "138268116111",
appId: "1:138268116111:web:419ac3a40392ff1c651cfa",
});
const messaging = firebase.messaging();
// Handle background messages
messaging.onBackgroundMessage(function (payload) {
console.log("Background message received:", payload);
if (payload?.notification) {
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
icon: "/images/nc-withbg.png"
};
// Only show when page is not focused
self.registration.showNotification(notificationTitle, notificationOptions);
}
});
self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SHOW_NOTIFICATION") {
const payload = event.data.payload;
const notificationTitle = payload.notification?.title || "Notification";
const notificationOptions = {
body: payload.notification?.body || "",
icon: "/images/nc-withbg.png"
};
self.registration.showNotification(notificationTitle, notificationOptions);
}
});
// Optional: Handle notification click
self.addEventListener("notificationclick", function(event) {
event.notification.close();
event.waitUntil(
clients.openWindow("/") // redirect to home or custom URL
);
});