39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
self.addEventListener("push", (event) => {
|
|
if (!event.data) return;
|
|
|
|
const payload = event.data.json();
|
|
if (payload.type !== "message") return;
|
|
|
|
const channel = payload.data?.channel || {};
|
|
|
|
const isInGuild = channel.type === "GUILD_TEXT" || channel.type === "GUILD_VOICE";
|
|
const channelType = isInGuild ? channel.type === "GUILD_TEXT" ? "Text" : "Voice" : "";
|
|
const title = `${payload.data?.author ?? "Unknown"}${isInGuild ? ` (#${channel.name}, ${channelType} Channels)` : ""}`;
|
|
|
|
const options = {
|
|
body: payload.data?.content || "",
|
|
icon: payload.data?.avatar || null,
|
|
tag: `channel-${channel.id}`,
|
|
data: { channelId: channel.id, isInGuild },
|
|
};
|
|
|
|
event.waitUntil(self.registration.showNotification(title, options));
|
|
});
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
event.notification.close();
|
|
|
|
const payload = event.notification.data;
|
|
const targetUrl = `/channels/${!payload.isInGuild ? "@me/" : ""}${payload.channelId}` || "/";
|
|
|
|
event.waitUntil(
|
|
clients.matchAll({ type: "window", includeUncontrolled: true }).then((clientList) => {
|
|
for (const client of clientList) {
|
|
if (client.url === targetUrl && "focus" in client) {
|
|
return client.focus();
|
|
}
|
|
}
|
|
return clients.openWindow(targetUrl);
|
|
})
|
|
);
|
|
}); |