only show notifications for low battery
This commit is contained in:
61
sw.js
61
sw.js
@@ -134,6 +134,17 @@ self.addEventListener('push', event => {
|
||||
};
|
||||
|
||||
console.log('[ServiceWorker] Preparing message payload:', messagePayload);
|
||||
|
||||
// Check if this is a low battery alert that needs a notification
|
||||
const isBatteryAlert = messagePayload.batteryLevel !== undefined &&
|
||||
messagePayload.batteryLevel < 50; // Use the same threshold as in the app
|
||||
|
||||
if (isBatteryAlert) {
|
||||
console.log(`[ServiceWorker] Low battery alert detected: ${messagePayload.batteryLevel}%`);
|
||||
// Change notification title/body for battery alerts
|
||||
pushData.title = 'Flic Button Low Battery';
|
||||
pushData.body = `Battery level is ${messagePayload.batteryLevel}%. Please replace batteries soon.`;
|
||||
}
|
||||
|
||||
// Send message to all open PWA windows controlled by this SW
|
||||
event.waitUntil(
|
||||
@@ -143,12 +154,16 @@ self.addEventListener('push', event => {
|
||||
}).then(clientList => {
|
||||
if (!clientList || clientList.length === 0) {
|
||||
console.log('[ServiceWorker] No client windows found to send message to.');
|
||||
// If no window is open, we MUST show a notification
|
||||
return self.registration.showNotification(pushData.title, {
|
||||
body: pushData.body,
|
||||
icon: '/icons/android-chrome-192x192.png', // Updated path
|
||||
data: pushData.data // Pass data if needed when notification is clicked
|
||||
});
|
||||
// If no window is open AND this is a battery alert, show a notification
|
||||
if (isBatteryAlert) {
|
||||
return self.registration.showNotification(pushData.title, {
|
||||
body: pushData.body,
|
||||
icon: '/icons/android-chrome-192x192.png', // Updated path
|
||||
data: pushData.data // Pass data if needed when notification is clicked
|
||||
});
|
||||
}
|
||||
// Otherwise, don't show notification for regular button presses
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
// Post message to each client with improved reliability
|
||||
@@ -160,7 +175,6 @@ self.addEventListener('push', event => {
|
||||
client.postMessage(messagePayload);
|
||||
messageSent = 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) {
|
||||
@@ -170,32 +184,19 @@ self.addEventListener('push', event => {
|
||||
});
|
||||
|
||||
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
|
||||
});
|
||||
// Only show a notification if this is a battery alert
|
||||
if (isBatteryAlert) {
|
||||
return self.registration.showNotification(pushData.title, {
|
||||
body: pushData.body,
|
||||
icon: '/icons/android-chrome-192x192.png',
|
||||
data: pushData.data
|
||||
});
|
||||
}
|
||||
// For regular button presses, don't show notifications
|
||||
return Promise.resolve();
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
// --- Show a notification (Important!) ---
|
||||
// Push notifications generally REQUIRE showing a notification to the user
|
||||
// unless the PWA is already in the foreground AND handles the event visually.
|
||||
// It's safer to always show one unless you have complex foreground detection.
|
||||
/* This part is now handled inside the clients.matchAll promise */
|
||||
/*
|
||||
const notificationOptions = {
|
||||
body: pushData.body,
|
||||
icon: './icons/android-chrome-192x192.png', // Optional: path to an icon
|
||||
data: pushData.data // Attach data if needed when notification is clicked
|
||||
};
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(pushData.title, notificationOptions)
|
||||
);
|
||||
*/
|
||||
});
|
||||
|
||||
// This helps with navigation after app is installed
|
||||
|
||||
Reference in New Issue
Block a user