diff --git a/mac/VibeTunnel/Core/Services/NotificationService.swift b/mac/VibeTunnel/Core/Services/NotificationService.swift index e9c0ab33..a91e77be 100644 --- a/mac/VibeTunnel/Core/Services/NotificationService.swift +++ b/mac/VibeTunnel/Core/Services/NotificationService.swift @@ -67,6 +67,13 @@ final class NotificationService: NSObject { /// Start monitoring server events func start() async { logger.info("🚀 NotificationService.start() called") + + // Check if notifications are enabled in config + guard configManager.notificationsEnabled else { + logger.info("📴 Notifications are disabled in config, skipping SSE connection") + return + } + guard serverManager.isRunning else { logger.warning("🔴 Server not running, cannot start notification service") return diff --git a/web/src/client/services/push-notification-service.ts b/web/src/client/services/push-notification-service.ts index 20f34df4..21a2b2e6 100644 --- a/web/src/client/services/push-notification-service.ts +++ b/web/src/client/services/push-notification-service.ts @@ -94,6 +94,9 @@ export class PushNotificationService { // Monitor permission changes this.monitorPermissionChanges(); + // Auto-resubscribe if notifications were previously enabled + await this.autoResubscribe(); + this.initialized = true; logger.log('push notification service initialized'); } catch (error) { @@ -159,6 +162,38 @@ export class PushNotificationService { }); } + /** + * Auto-resubscribe if notifications were previously enabled + */ + private async autoResubscribe(): Promise { + try { + // Load saved preferences + const preferences = await this.loadPreferences(); + + // Check if notifications were previously enabled + if (preferences.enabled) { + // Check if we have permission but no subscription + const permission = this.getPermission(); + if (permission === 'granted' && !this.pushSubscription) { + logger.log('Auto-resubscribing to push notifications based on saved preferences'); + + // Attempt to resubscribe + const subscription = await this.subscribe(); + if (subscription) { + logger.log('Successfully auto-resubscribed to push notifications'); + } else { + logger.warn('Failed to auto-resubscribe, user will need to manually enable'); + // Update preferences to reflect the failed state + preferences.enabled = false; + await this.savePreferences(preferences); + } + } + } + } catch (error) { + logger.error('Error during auto-resubscribe:', error); + } + } + /** * Request notification permission from user */