Refactor loading and error handling in DesktopHomePage; improve notification click handling in service worker

This commit is contained in:
amirrezaghabeli
2025-10-05 10:23:29 +03:30
parent f74f20a4f8
commit d050aab728
3 changed files with 117 additions and 58 deletions
+37 -13
View File
@@ -14,25 +14,49 @@ firebase.initializeApp({
const messaging = firebase.messaging();
// Handle background messages
messaging.onBackgroundMessage(async (payload) => {
// 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,
};
// 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);
});
// self.registration.showNotification(notificationTitle, notificationOptions);
// });
// Open or focus the app when a notification is clicked
self.addEventListener('notificationclick', (event) => {
event.notification.close();
const targetUrl = 'https://app.opplens.com/notification';
// 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(
self.clients.openWindow(targetUrl)
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);
}
})
);
});