import 'dart:io'; import 'package:flutter_foreground_task/flutter_foreground_task.dart'; /// Foreground Service Android — garde le processus vivant quand écran verrouillé. /// iOS : UIBackgroundModes audio dans Info.plist suffit. class GlassesBackgroundService { static Future initialize() async { if (!Platform.isAndroid) return; FlutterForegroundTask.init( androidNotificationOptions: AndroidNotificationOptions( channelId: 'myinfomate_glasses', channelName: 'MyInfoMate Guide', channelDescription: 'Votre guide de visite est actif', channelImportance: NotificationChannelImportance.LOW, priority: NotificationPriority.LOW, ), iosNotificationOptions: const IOSNotificationOptions( showNotification: false, ), foregroundTaskOptions: ForegroundTaskOptions( eventAction: ForegroundTaskEventAction.nothing(), autoRunOnBoot: false, allowWakeLock: true, ), ); } static Future start() async { if (!Platform.isAndroid) return; if (await FlutterForegroundTask.isRunningService) return; await FlutterForegroundTask.startService( serviceId: 1001, notificationTitle: 'Guide de visite actif', notificationText: 'Dites "visite" pour parler à votre guide', callback: _taskCallback, ); } static Future stop() async { if (!Platform.isAndroid) return; await FlutterForegroundTask.stopService(); } static Future updateNotification(String text) async { if (!Platform.isAndroid) return; await FlutterForegroundTask.updateService( notificationTitle: 'Guide de visite actif', notificationText: text, ); } } // Doit être top-level pour flutter_foreground_task @pragma('vm:entry-point') void _taskCallback() { FlutterForegroundTask.setTaskHandler(_GlassesTaskHandler()); } class _GlassesTaskHandler extends TaskHandler { @override Future onStart(DateTime timestamp, TaskStarter starter) async {} @override void onRepeatEvent(DateTime timestamp) {} @override Future onDestroy(DateTime timestamp) async {} }