fix for handling messages
This commit is contained in:
@@ -80,6 +80,14 @@ async function initialize() {
|
||||
[config.FLIC_ACTIONS.DOUBLE_CLICK]: playerManager.previousPlayer,
|
||||
[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);
|
||||
|
||||
// 8. Setup Service Worker (which also initializes Flic)
|
||||
|
||||
@@ -250,7 +250,7 @@ export function handleFlicAction(action, buttonId, timestamp, batteryLevel) {
|
||||
console.log(`[PushFlic] Received Action: ${action} from Button: ${buttonId} battery: ${batteryLevel}% at ${timestamp}`);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
@@ -262,12 +262,19 @@ export function handleFlicAction(action, buttonId, timestamp, batteryLevel) {
|
||||
|
||||
// Find the registered handler for this action
|
||||
const handler = actionHandlers[action];
|
||||
|
||||
if (handler && typeof handler === 'function') {
|
||||
console.log(`[PushFlic] Executing handler for ${action}`);
|
||||
// Execute the handler registered in app.js
|
||||
handler(); // Use the handler function directly instead of hardcoded function calls
|
||||
try {
|
||||
// Execute the handler registered in app.js
|
||||
handler();
|
||||
// Log success
|
||||
console.log(`[PushFlic] Successfully executed handler for ${action}`);
|
||||
} catch (error) {
|
||||
console.error(`[PushFlic] Error executing handler for ${action}:`, error);
|
||||
}
|
||||
} else {
|
||||
console.warn(`[PushFlic] No handler registered for action: ${action}`);
|
||||
console.warn(`[PushFlic] No handler registered for action: ${action}. Available handlers:`, Object.keys(actionHandlers));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,18 +55,18 @@ export function setupServiceWorker(messageHandler) {
|
||||
// Set up global message event listener on window object
|
||||
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', () => {
|
||||
navigator.serviceWorker.register('/sw.js')
|
||||
.then(registration => {
|
||||
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
|
||||
if (navigator.serviceWorker.controller) {
|
||||
console.log('Service worker already controlling the page, setting up message listener');
|
||||
navigator.serviceWorker.controller.addEventListener('message', messageHandler);
|
||||
}
|
||||
|
||||
// Initialize Flic integration
|
||||
@@ -80,10 +80,6 @@ export function setupServiceWorker(messageHandler) {
|
||||
// Listen for SW controller changes
|
||||
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
||||
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 {
|
||||
|
||||
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