fixed message handler

This commit is contained in:
cpu
2025-03-29 06:22:55 +01:00
parent ad96260d91
commit c71526b6c3
2 changed files with 42 additions and 7 deletions

20
sw.js
View File

@@ -142,12 +142,24 @@ self.addEventListener('push', event => {
});
}
// Post message to each client
// Post message to each client with improved reliability
let messageSent = false;
clientList.forEach(client => {
const sendPromises = clientList.map(client => {
console.log(`[ServiceWorker] Posting message to client: ${client.id}`, messagePayload);
client.postMessage(messagePayload);
messageSent = true; // Mark that we at least tried to send a message
try {
// Try to send the message and mark it as sent
client.postMessage(messagePayload);
messageSent = true;
// Also try to focus the client to ensure it gets the message
if ('focus' in client) {
return client.focus().then(() => true);
}
return Promise.resolve(true);
} catch (error) {
console.error('[ServiceWorker] Error posting message to client:', error);
return Promise.resolve(false);
}
});
// Decide whether to still show a notification even if a window is open.