fix for handling messages

This commit is contained in:
cpu
2025-03-29 06:34:38 +01:00
parent c71526b6c3
commit 26c6a74641
4 changed files with 29 additions and 22 deletions

16
sw.js
View File

@@ -151,10 +151,8 @@ self.addEventListener('push', event => {
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);
}
// REMOVED: Don't try to focus the client as it causes errors
// Just return true to indicate message was sent
return Promise.resolve(true);
} catch (error) {
console.error('[ServiceWorker] Error posting message to client:', error);
@@ -162,17 +160,15 @@ self.addEventListener('push', event => {
}
});
// Decide whether to still show a notification even if a window is open.
// Generally good practice unless you are SURE the app will handle it visibly.
// You might choose *not* to show a notification if a client was found and focused.
// For simplicity here, we'll still show one. Adjust as needed.
if (!messageSent) { // Only show notification if no message was sent? Or always show?
return Promise.all(sendPromises).then(() => {
// Always show a notification unless we're sure the app can handle it visibly
// This ensures the user gets notified even if the app doesn't process the message
return self.registration.showNotification(pushData.title, {
body: pushData.body,
icon: '/icons/android-chrome-192x192.png',
data: pushData.data
});
}
});
})
);