-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af5d8d3
commit fc967ef
Showing
7 changed files
with
288 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,84 @@ | ||
// Any other custom service worker logic can go here. | ||
// Define a cache name for versioning your cache | ||
const CACHE_NAME = 'my-cache-v1'; | ||
|
||
self.addEventListener("push", async function (event) { | ||
// Cache assets during the install phase | ||
self.addEventListener('install', (event) => { | ||
console.log('[Service Worker] Install'); | ||
event.waitUntil( | ||
caches.open(CACHE_NAME).then((cache) => { | ||
return cache.addAll([ | ||
'/', | ||
'/index.html', | ||
'/styles.css', | ||
]); | ||
}).then(() => self.skipWaiting()) // Skip waiting to activate new service worker immediately | ||
); | ||
}); | ||
|
||
// Clean up old caches during the activate phase | ||
self.addEventListener('activate', (event) => { | ||
console.log('[Service Worker] Activate'); | ||
event.waitUntil( | ||
caches.keys().then((cacheNames) => { | ||
return Promise.all( | ||
cacheNames.map((cacheName) => { | ||
if (cacheName !== CACHE_NAME) { | ||
console.log('[Service Worker] Deleting old cache:', cacheName); | ||
return caches.delete(cacheName); | ||
} | ||
}) | ||
); | ||
}).then(() => self.clients.claim()) // Take control of all clients as soon as active | ||
); | ||
}); | ||
|
||
|
||
|
||
// Listen for push events and display notifications | ||
self.addEventListener('push', (event) => { | ||
console.log('[Service Worker] Push Received', event); | ||
if (event.data) { | ||
const data = event.data.json(); | ||
const notificationOptions = { | ||
body: data.body || "Sample Body", | ||
tag: data.external_id || "default-tag", | ||
body:data.body || 'Message Received from OpenELIS', | ||
tag: data.external_id || 'default-tag', | ||
icon: 'images/openelis_logo.png', | ||
}; | ||
|
||
event.waitUntil( | ||
self.registration.showNotification( | ||
"OpenELIS Test Message Received", | ||
notificationOptions, | ||
), | ||
self.registration.showNotification('OpenELIS Test Message Received', notificationOptions) | ||
); | ||
} | ||
}); | ||
|
||
// This allows the web app to trigger skipWaiting via | ||
// registration.waiting.postMessage({type: 'SKIP_WAITING'}) | ||
self.addEventListener("message", (event) => { | ||
if (event.data && event.data.type === "SKIP_WAITING") { | ||
// Notification click event listener | ||
// self.addEventListener("notificationclick", (event) => { | ||
// console.log('Notification clicked'); | ||
// event.notification.close(); // Close the notification popout | ||
|
||
// event.waitUntil( | ||
// clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => { | ||
// // Check if any client (tab or window) is already open | ||
// for (let i = 0; i < clientList.length; i++) { | ||
// const client = clientList[i]; | ||
// if (client.url === 'https://www.youtube.com/' && 'focus' in client) { | ||
// return client.focus(); | ||
// } | ||
// } | ||
|
||
// // If no client is open, open a new window | ||
// if (clients.openWindow) { | ||
// return clients.openWindow('https://www.youtube.com/'); | ||
// } | ||
// }) | ||
// ); | ||
// }); | ||
|
||
|
||
|
||
// Handle messages from clients | ||
self.addEventListener('message', (event) => { | ||
if (event.data && event.data.type === 'SKIP_WAITING') { | ||
self.skipWaiting(); | ||
} | ||
}); | ||
|
||
// self.addEventListener('push', (e) => { | ||
// const data = e.data?.json(); | ||
// if (data) { | ||
// self.registration.showNotification(data.title, { | ||
// body: data.body, | ||
// }); | ||
// } | ||
// }); | ||
|
||
// self.addEventListener('notificationclick', (e) => { | ||
// e.notification.close(); | ||
// e.waitUntil(focusOrOpenWindow()); | ||
// }); | ||
|
||
// async function focusOrOpenWindow() { | ||
// const url = new URL('/', self.location.origin).href; | ||
|
||
// const allWindows = await self.clients.matchAll({ | ||
// type: 'window', | ||
// }); | ||
// const appWindow = allWindows.find((w) => w.url === url); | ||
|
||
// if (appWindow) { | ||
// return appWindow.focus(); | ||
// } else { | ||
// return self.clients.openWindow(url); | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.