fix for handling messages
This commit is contained in:
16
sw.js
16
sw.js
@@ -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
|
||||
});
|
||||
}
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user