WIP: новая схема сообщений и push (не проверено)

This commit is contained in:
AidarKC
2026-04-19 20:41:58 +03:00
parent f0b560ec06
commit cc59bd18ee
27 changed files with 1668 additions and 94 deletions
+22 -6
View File
@@ -12,26 +12,42 @@ async function broadcastToClients(payload) {
}
self.addEventListener('push', (event) => {
let body = 'Новое сообщение SHiNE';
let body = '';
let rawText = '';
let kind = '';
let fromLogin = '';
try {
if (event.data) {
const text = event.data.text();
rawText = text || '';
body = rawText || body;
try {
const json = JSON.parse(rawText || '{}');
kind = String(json.kind || '');
body = String(json.text || '');
fromLogin = String(json.fromLogin || '');
} catch {
body = rawText || '';
}
}
} catch {
// ignore
}
event.waitUntil(Promise.all([
self.registration.showNotification('SHiNE: входящее сообщение', {
body,
const shouldNotify = kind === 'new_message' || (!kind && body);
const notifyPromise = shouldNotify
? self.registration.showNotification('SHiNE: входящее сообщение', {
body: body || (fromLogin ? `Вам пришло сообщение от ${fromLogin}` : 'Вам пришло сообщение'),
tag: 'shine-direct-message',
renotify: true,
}),
})
: Promise.resolve();
event.waitUntil(Promise.all([
notifyPromise,
broadcastToClients({
kind,
body,
fromLogin,
rawText,
receivedAt: Date.now(),
}),