Skip to content

Commit

Permalink
Merge pull request I-TECH-UW#1208 from konavivekramakrishna/add#WP2
Browse files Browse the repository at this point in the history
add Push Notification Service🔔
  • Loading branch information
mozzy11 authored Aug 22, 2024
2 parents 6e4c62b + 50d0c2b commit 6a31d09
Show file tree
Hide file tree
Showing 20 changed files with 1,076 additions and 311 deletions.
4 changes: 2 additions & 2 deletions frontend/public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"short_name": "OpenELIS",
"name": "OpenELIS Global",
"icons": [
{
"src": "images/favicon-16x16.png",
Expand Down
85 changes: 85 additions & 0 deletions frontend/public/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Define a cache name for versioning your cache
const CACHE_NAME = "my-cache-v1";

// 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 || "Message Received from OpenELIS",
tag: data.external_id || "default-tag",
icon: "images/openelis_logo.png",
};

event.waitUntil(
self.registration.showNotification(
"OpenELIS Message Received",
notificationOptions,
),
);
}
});

// 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();
}
});
2 changes: 1 addition & 1 deletion frontend/src/components/Style.css
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ button {
}

#mainHeader .cds--side-nav__link {
pointer-events: none
pointer-events: none;
}

@media screen and (max-width: 792px) {
Expand Down
294 changes: 294 additions & 0 deletions frontend/src/components/notifications/NoNotificationSVG.jsx

Large diffs are not rendered by default.

Loading

0 comments on commit 6a31d09

Please sign in to comment.