62 lines
2.2 KiB
JavaScript
62 lines
2.2 KiB
JavaScript
importScripts("https://www.gstatic.com/firebasejs/9.22.2/firebase-app-compat.js");
|
|
importScripts("https://www.gstatic.com/firebasejs/9.22.2/firebase-messaging-compat.js");
|
|
|
|
firebase.initializeApp({
|
|
apiKey: "AIzaSyCTjdsk2jE34IfnvSKP1JMTIf_Abd7tbt0",
|
|
authDomain: "opplens-270d1.firebaseapp.com",
|
|
projectId: "opplens-270d1",
|
|
storageBucket: "opplens-270d1.firebasestorage.app",
|
|
messagingSenderId: "6923326255",
|
|
appId: "1:6923326255:web:c71763ece06aaaf3ed87da",
|
|
measurementId: "G-M5HQR4YZVS"
|
|
});
|
|
|
|
const messaging = firebase.messaging();
|
|
|
|
// Handle background messages
|
|
// messaging.onBackgroundMessage(async (payload) => {
|
|
|
|
// const notificationTitle = payload.notification.title;
|
|
// const notificationOptions = {
|
|
// body: payload.notification.body,
|
|
// badge: '/icons/Icon-192.svg',
|
|
// icon: '/icons/notification.svg',
|
|
// data: payload.data,
|
|
// click_action: payload.notification.click_action,
|
|
// };
|
|
|
|
// self.registration.showNotification(notificationTitle, notificationOptions);
|
|
// });
|
|
|
|
// Open or focus the app when a notification is clicked
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close();
|
|
|
|
// Get the URL from the data payload, defaulting to the notification page
|
|
const urlToOpen = event.notification.data?.url || '/notification';
|
|
const baseUrl = 'https://app.opplens.com';
|
|
const targetUrl = urlToOpen.startsWith('http') ? urlToOpen : `${baseUrl}${urlToOpen}`;
|
|
|
|
console.log('Opening URL:', targetUrl);
|
|
|
|
event.waitUntil(
|
|
clients.matchAll({
|
|
type: 'window',
|
|
includeUncontrolled: true,
|
|
}).then((clientList) => {
|
|
// Find an open client (any tab/window belonging to your site)
|
|
const focusedClient = clientList.find(client => {
|
|
return new URL(client.url).origin === location.origin;
|
|
});
|
|
|
|
if (focusedClient) {
|
|
// If a client is open, navigate it to the new URL and focus it
|
|
focusedClient.navigate(urlToOpen);
|
|
return focusedClient.focus();
|
|
} else {
|
|
// Otherwise, open a new window
|
|
return clients.openWindow(targetUrl);
|
|
}
|
|
})
|
|
);
|
|
}); |