2022-06-01 21:56:37 +00:00
|
|
|
import type { NotificationPayload } from "~/utils/web-push.server";
|
|
|
|
import { addBadge } from "~/utils/pwa.client";
|
|
|
|
|
2022-06-11 13:13:28 +00:00
|
|
|
declare const self: ServiceWorkerGlobalScope;
|
2022-06-01 21:56:37 +00:00
|
|
|
|
|
|
|
const defaultOptions: NotificationOptions = {
|
|
|
|
icon: "/icons/android-chrome-192x192.png",
|
|
|
|
badge: "/icons/android-chrome-48x48.png",
|
|
|
|
dir: "auto",
|
|
|
|
image: undefined,
|
|
|
|
silent: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default async function handlePush(event: PushEvent) {
|
2022-06-12 21:17:22 +00:00
|
|
|
const payload: NotificationPayload = event.data!.json();
|
2022-06-01 21:56:37 +00:00
|
|
|
const options = Object.assign({}, defaultOptions, payload);
|
2022-06-12 21:17:22 +00:00
|
|
|
|
2022-06-12 21:32:57 +00:00
|
|
|
const revalidateChannel = new BroadcastChannel("revalidate");
|
|
|
|
// should revalidate just "/messages" and `/messages/${encodeURIComponent(payload.data.recipient)}`
|
|
|
|
revalidateChannel.postMessage("revalidateLoaderData");
|
|
|
|
revalidateChannel.close();
|
|
|
|
|
2022-06-19 15:57:51 +00:00
|
|
|
await self.registration.showNotification(payload.title, options);
|
|
|
|
await addBadge(1);
|
2022-06-01 21:56:37 +00:00
|
|
|
}
|