fixed message handler
This commit is contained in:
20
sw.js
20
sw.js
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user