notify of incoming messages and show in app notifications

This commit is contained in:
m5r
2022-06-12 23:17:22 +02:00
parent a77f02189e
commit ceaadc4f99
7 changed files with 268 additions and 44 deletions

View File

@ -12,8 +12,17 @@ const defaultOptions: NotificationOptions = {
};
export default async function handlePush(event: PushEvent) {
const { title, ...payload }: NotificationPayload = event.data!.json();
const payload: NotificationPayload = event.data!.json();
const options = Object.assign({}, defaultOptions, payload);
await self.registration.showNotification(title, options);
await addBadge(1);
const clients = await self.clients.matchAll({ type: "window" });
const hasOpenTab = clients.some((client) => client.focused === true);
if (hasOpenTab) {
const channel = new BroadcastChannel("notifications");
channel.postMessage(JSON.stringify(payload));
channel.close();
} else {
await self.registration.showNotification(payload.title, options);
await addBadge(1);
}
}