shellphone.app/app/service-worker/notification-click.ts
m5r 1e9b7a8aa2 * fix "dev:build" watch mode
* remove cross-env
* append build hash to service worker cache names for easy purge
2022-06-11 15:13:28 +02:00

33 lines
895 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { removeBadge } from "~/utils/pwa.client";
declare const self: ServiceWorkerGlobalScope;
// noinspection TypeScriptUnresolvedVariable
export default async function handleNotificationClick(event: NotificationEvent) {
console.debug("On notification click: ", event.notification.tag);
// Android doesnt close the notification when you click on it
// See: http://crbug.com/463146
event.notification.close();
await removeBadge();
const url = getUrl(event.notification.data);
return self.clients.openWindow?.(url);
}
type NotificationData = {
recipient: string;
type: "message" | "incoming-call";
};
function getUrl(data: NotificationData) {
const recipient = encodeURIComponent(data.recipient);
switch (data.type) {
case "message":
return `/messages/${recipient}`;
case "incoming-call":
return `/incoming-call/${recipient}`;
default:
return "/messages";
}
}