Format emojis in the service worker directly

This commit is contained in:
nimbleghost 2023-05-31 18:27:32 +02:00
parent 44913c1668
commit 4648f83669
12 changed files with 85 additions and 112 deletions

View file

@ -4,6 +4,7 @@ import { NavigationRoute, registerRoute } from "workbox-routing";
import { NetworkFirst } from "workbox-strategies";
import { getDbAsync } from "../src/app/getDb";
import { formatMessage, formatTitleWithDefault } from "../src/app/notificationUtils";
// See WebPushWorker, this is to play a sound on supported browsers,
// if the app is in the foreground
@ -27,11 +28,11 @@ self.addEventListener("pushsubscriptionchange", (event) => {
});
self.addEventListener("push", (event) => {
console.log("[ServiceWorker] Received Web Push Event", { event });
// server/types.go webPushPayload
const data = event.data.json();
console.log("[ServiceWorker] Received Web Push Event", { event, data });
const { formatted_title: formattedTitle, subscription_id: subscriptionId, message } = data;
const { subscription_id: subscriptionId, message } = data;
broadcastChannel.postMessage(message);
event.waitUntil(
@ -53,9 +54,9 @@ self.addEventListener("push", (event) => {
db.subscriptions.update(subscriptionId, {
last: message.id,
}),
self.registration.showNotification(formattedTitle, {
self.registration.showNotification(formatTitleWithDefault(message, message.topic), {
tag: subscriptionId,
body: message.message,
body: formatMessage(message),
icon: "/static/images/ntfy.png",
data,
}),
@ -106,6 +107,7 @@ precacheAndRoute(self.__WB_MANIFEST);
cleanupOutdatedCaches();
// to allow work offline
registerRoute(new NavigationRoute(createHandlerBoundToURL("/")));
registerRoute(({ url }) => url.pathname === "/config.js", new NetworkFirst());
if (import.meta.env.MODE !== "development") {
registerRoute(new NavigationRoute(createHandlerBoundToURL("/")));
registerRoute(({ url }) => url.pathname === "/config.js", new NetworkFirst());
}