limit notifications for low battery

This commit is contained in:
cpu
2025-03-29 07:05:55 +01:00
parent 8261ee3326
commit eb427d7fdd
2 changed files with 177 additions and 12 deletions

View File

@@ -4,6 +4,61 @@ import { getPublicVapidKey, BACKEND_URL, FLIC_BUTTON_ID, FLIC_ACTIONS, FLIC_BATT
let pushSubscription = null; // Keep track locally if needed
let actionHandlers = {}; // Store handlers for different Flic actions
let lastBatteryWarningTimestamp = 0; // Track when last battery warning was shown
const BATTERY_WARNING_STORAGE_KEY = 'last-battery-warning-timestamp';
// On initialization, try to load timestamp from localStorage
try {
const storedTimestamp = localStorage.getItem(BATTERY_WARNING_STORAGE_KEY);
if (storedTimestamp) {
lastBatteryWarningTimestamp = parseInt(storedTimestamp, 10);
console.log('[PushFlic] Loaded battery warning timestamp from localStorage:', new Date(lastBatteryWarningTimestamp));
}
} catch (error) {
console.error('[PushFlic] Error loading timestamp from localStorage:', error);
}
// Also try to get timestamp from service worker
function syncTimestampWithServiceWorker() {
if ('serviceWorker' in navigator && navigator.serviceWorker.controller) {
// First, register for battery timestamp messages
navigator.serviceWorker.addEventListener('message', event => {
if (event.data && event.data.type === 'battery-timestamp') {
const swTimestamp = event.data.timestamp;
console.log('[PushFlic] Received timestamp from SW:', new Date(swTimestamp));
// Use the most recent timestamp (either from SW or local)
if (swTimestamp > lastBatteryWarningTimestamp) {
lastBatteryWarningTimestamp = swTimestamp;
saveTimestampToLocalStorage(swTimestamp);
} else if (lastBatteryWarningTimestamp > swTimestamp) {
// Update the service worker with our more recent timestamp
navigator.serviceWorker.controller.postMessage({
type: 'update-battery-timestamp',
timestamp: lastBatteryWarningTimestamp
});
}
}
});
// Ask service worker for its timestamp
navigator.serviceWorker.controller.postMessage({
type: 'get-battery-timestamp'
});
}
}
// Call this on init
setTimeout(syncTimestampWithServiceWorker, 1000);
// Save timestamp to localStorage
function saveTimestampToLocalStorage(timestamp) {
try {
localStorage.setItem(BATTERY_WARNING_STORAGE_KEY, timestamp.toString());
console.log('[PushFlic] Saved timestamp to localStorage:', new Date(timestamp));
} catch (error) {
console.error('[PushFlic] Error saving timestamp to localStorage:', error);
}
}
// --- Helper Functions ---
@@ -79,8 +134,20 @@ function showBatteryWarning(batteryLevel) {
return;
}
// Update timestamp
lastBatteryWarningTimestamp = now;
// Save to localStorage
saveTimestampToLocalStorage(now);
// Also update service worker
if ('serviceWorker' in navigator && navigator.serviceWorker.controller) {
navigator.serviceWorker.controller.postMessage({
type: 'update-battery-timestamp',
timestamp: now
});
}
// Show the notification
console.log(`[PushFlic] Low battery detected: ${batteryLevel}%`);
@@ -328,6 +395,9 @@ export function initPushFlic(handlers) {
console.warn('[PushFlic] No action handlers provided to initPushFlic, actions will not work!');
}
// Sync battery timestamp with service worker
syncTimestampWithServiceWorker();
// Auto-subscribe when permission is granted
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {