Skip to content

Commit

Permalink
Add comms with SW
Browse files Browse the repository at this point in the history
  • Loading branch information
captainbrosset committed Nov 4, 2024
1 parent 5a8239d commit 43ab653
Showing 1 changed file with 4 additions and 67 deletions.
71 changes: 4 additions & 67 deletions incoming-call-notifications/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,18 @@ onactivate = () => {
return clients.claim();
};

onmessage = (messageEvent) => {
registration
.showNotification(`ServiceWorkerGlobalScope Title ${messageEvent.data}`, {
body: `ServiceWorkerGlobalScope Body ${messageEvent.data}`,
icon: "../resources/happy.jpg",
})
.then(() => {
messageEvent.source.postMessage(
"ServiceWorkerGlobalScope showNotification() succeeded."
);
})
.catch((error) => {
messageEvent.source.postMessage(
`ServiceWorkerGlobalScope showNotification() failed: ${error}.`
);
});
onfetch = (fetchEvent) => {
console.log(fetchEvent.request.url);
fetchEvent.respondWith(fetch(fetchEvent.request));
};

onnotificationclick = (event) => {
const notification = event.notification;

clients.matchAll().then((resultList) => {
resultList.forEach((client) => {
client.postMessage(
`ServiceWorkerGlobalScope 'click' event for: ${
notification.title
} , timestamp: ${new Date(
notification.timestamp
)}, requireInteraction: ${notification.requireInteraction}, silent: ${
notification.silent
}`
);
notification.close();
});
});

if (event.action === "open_window") {
event.waitUntil(
new Promise((resolve) => {
setTimeout(() => {
clients.openWindow("on-click.html");
resolve();
}, 0);
})
);
} else {
// Focus existing client.
event.waitUntil(
clients.matchAll().then((resultList) => {
if (resultList.length > 0) {
return resultList[0].focus();
}
})
);
}
};

onnotificationclose = (event) => {
clients.matchAll().then((resultList) => {
resultList.forEach((client) => {
const notification = event.notification;
client.postMessage(
`ServiceWorkerGlobalScope 'close' event for: ${
notification.title
} , timestamp: ${new Date(
notification.timestamp
)}, requireInteraction: ${notification.requireInteraction}, silent: ${
notification.silent
}`
`Service-worker: Action '${event.action}' for: ${notification.title}`
);
});
});
};

onfetch = (fetchEvent) => {
console.log(fetchEvent.request.url);
fetchEvent.respondWith(fetch(fetchEvent.request));
};

0 comments on commit 43ab653

Please sign in to comment.