71 lines
2.1 KiB
Dart
71 lines
2.1 KiB
Dart
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<void> 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<void> 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<void> stop() async {
|
|
if (!Platform.isAndroid) return;
|
|
await FlutterForegroundTask.stopService();
|
|
}
|
|
|
|
static Future<void> 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<void> onStart(DateTime timestamp, TaskStarter starter) async {}
|
|
|
|
@override
|
|
void onRepeatEvent(DateTime timestamp) {}
|
|
|
|
@override
|
|
Future<void> onDestroy(DateTime timestamp) async {}
|
|
}
|