web: group notifications per chat thread
This commit is contained in:
@@ -26,15 +26,34 @@ export async function showNotificationViaServiceWorker(params: {
|
|||||||
if (!registration) {
|
if (!registration) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
const tag = `chat-${params.chatId}`;
|
||||||
|
const active = await registration.getNotifications({ tag });
|
||||||
|
const prevData = active[0]?.data as
|
||||||
|
| {
|
||||||
|
unreadCount?: number;
|
||||||
|
previews?: string[];
|
||||||
|
}
|
||||||
|
| undefined;
|
||||||
|
const unreadCount = Math.max(0, Number(prevData?.unreadCount ?? 0)) + 1;
|
||||||
|
const previews = [params.body, ...(prevData?.previews ?? [])]
|
||||||
|
.filter((item) => item && item.trim().length > 0)
|
||||||
|
.slice(0, 3);
|
||||||
|
const extraCount = Math.max(0, unreadCount - previews.length);
|
||||||
|
const groupedBody =
|
||||||
|
previews.length <= 1
|
||||||
|
? previews[0] ?? params.body
|
||||||
|
: `${previews.join("\n")}${extraCount > 0 ? `\n+${extraCount} more` : ""}`;
|
||||||
const url = `/?chat=${params.chatId}&message=${params.messageId}`;
|
const url = `/?chat=${params.chatId}&message=${params.messageId}`;
|
||||||
await registration.showNotification(params.title, {
|
await registration.showNotification(params.title, {
|
||||||
body: params.body,
|
body: groupedBody,
|
||||||
icon: params.image,
|
icon: params.image,
|
||||||
tag: `chat-${params.chatId}`,
|
tag,
|
||||||
data: {
|
data: {
|
||||||
chatId: params.chatId,
|
chatId: params.chatId,
|
||||||
messageId: params.messageId,
|
messageId: params.messageId,
|
||||||
url,
|
url,
|
||||||
|
unreadCount,
|
||||||
|
previews,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user