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

View File

@@ -80,6 +80,14 @@ async function initialize() {
[config.FLIC_ACTIONS.DOUBLE_CLICK]: playerManager.previousPlayer, [config.FLIC_ACTIONS.DOUBLE_CLICK]: playerManager.previousPlayer,
[config.FLIC_ACTIONS.HOLD]: gameActions.togglePauseResume, [config.FLIC_ACTIONS.HOLD]: gameActions.togglePauseResume,
}; };
// Log the registered handlers for debugging
console.log("Registering Flic action handlers:", {
"SingleClick": config.FLIC_ACTIONS.SINGLE_CLICK,
"DoubleClick": config.FLIC_ACTIONS.DOUBLE_CLICK,
"Hold": config.FLIC_ACTIONS.HOLD
});
serviceWorkerManager.setFlicActionHandlers(flicActionHandlers); serviceWorkerManager.setFlicActionHandlers(flicActionHandlers);
// 8. Setup Service Worker (which also initializes Flic) // 8. Setup Service Worker (which also initializes Flic)

View File

@@ -250,7 +250,7 @@ export function handleFlicAction(action, buttonId, timestamp, batteryLevel) {
console.log(`[PushFlic] Received Action: ${action} from Button: ${buttonId} battery: ${batteryLevel}% at ${timestamp}`); console.log(`[PushFlic] Received Action: ${action} from Button: ${buttonId} battery: ${batteryLevel}% at ${timestamp}`);
// Check if battery is below threshold and show warning if needed // Check if battery is below threshold and show warning if needed
if (batteryLevel < FLIC_BATTERY_THRESHOLD) { if (batteryLevel !== undefined && batteryLevel < FLIC_BATTERY_THRESHOLD) {
showBatteryWarning(batteryLevel); showBatteryWarning(batteryLevel);
} }
@@ -262,12 +262,19 @@ export function handleFlicAction(action, buttonId, timestamp, batteryLevel) {
// Find the registered handler for this action // Find the registered handler for this action
const handler = actionHandlers[action]; const handler = actionHandlers[action];
if (handler && typeof handler === 'function') { if (handler && typeof handler === 'function') {
console.log(`[PushFlic] Executing handler for ${action}`); console.log(`[PushFlic] Executing handler for ${action}`);
try {
// Execute the handler registered in app.js // Execute the handler registered in app.js
handler(); // Use the handler function directly instead of hardcoded function calls handler();
// Log success
console.log(`[PushFlic] Successfully executed handler for ${action}`);
} catch (error) {
console.error(`[PushFlic] Error executing handler for ${action}:`, error);
}
} else { } else {
console.warn(`[PushFlic] No handler registered for action: ${action}`); console.warn(`[PushFlic] No handler registered for action: ${action}. Available handlers:`, Object.keys(actionHandlers));
} }
} }

View File

@@ -55,18 +55,18 @@ export function setupServiceWorker(messageHandler) {
// Set up global message event listener on window object // Set up global message event listener on window object
window.addEventListener('message', handleServiceWorkerMessage); window.addEventListener('message', handleServiceWorkerMessage);
// Listen for messages FROM the Service Worker
// This is the main way messages from the service worker are received
navigator.serviceWorker.addEventListener('message', messageHandler);
window.addEventListener('load', () => { window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js') navigator.serviceWorker.register('/sw.js')
.then(registration => { .then(registration => {
console.log('ServiceWorker registered successfully.'); console.log('ServiceWorker registered successfully.');
// Listen for messages FROM the Service Worker
navigator.serviceWorker.addEventListener('message', messageHandler);
// Add an event listener that will work with service worker controlled clients // Add an event listener that will work with service worker controlled clients
if (navigator.serviceWorker.controller) { if (navigator.serviceWorker.controller) {
console.log('Service worker already controlling the page, setting up message listener'); console.log('Service worker already controlling the page, setting up message listener');
navigator.serviceWorker.controller.addEventListener('message', messageHandler);
} }
// Initialize Flic integration // Initialize Flic integration
@@ -80,10 +80,6 @@ export function setupServiceWorker(messageHandler) {
// Listen for SW controller changes // Listen for SW controller changes
navigator.serviceWorker.addEventListener('controllerchange', () => { navigator.serviceWorker.addEventListener('controllerchange', () => {
console.log('Service Worker controller changed, potentially updated.'); console.log('Service Worker controller changed, potentially updated.');
// Re-attach event listener to the new controller
if (navigator.serviceWorker.controller) {
navigator.serviceWorker.controller.addEventListener('message', messageHandler);
}
}); });
} else { } else {

16
sw.js
View File

@@ -151,10 +151,8 @@ self.addEventListener('push', event => {
client.postMessage(messagePayload); client.postMessage(messagePayload);
messageSent = true; messageSent = true;
// Also try to focus the client to ensure it gets the message // REMOVED: Don't try to focus the client as it causes errors
if ('focus' in client) { // Just return true to indicate message was sent
return client.focus().then(() => true);
}
return Promise.resolve(true); return Promise.resolve(true);
} catch (error) { } catch (error) {
console.error('[ServiceWorker] Error posting message to client:', 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. return Promise.all(sendPromises).then(() => {
// Generally good practice unless you are SURE the app will handle it visibly. // Always show a notification unless we're sure the app can handle it visibly
// You might choose *not* to show a notification if a client was found and focused. // This ensures the user gets notified even if the app doesn't process the message
// 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 self.registration.showNotification(pushData.title, { return self.registration.showNotification(pushData.title, {
body: pushData.body, body: pushData.body,
icon: '/icons/android-chrome-192x192.png', icon: '/icons/android-chrome-192x192.png',
data: pushData.data data: pushData.data
}); });
} });
}) })
); );