initForegroundTask method
Initialize the foreground service configuration
Implementation
Future<void> initForegroundTask() async {
debugPrint("[DEBUG] Initializing foreground task configuration at ${DateTime.now()}");
// Configure the foreground task
FlutterForegroundTask.init(
androidNotificationOptions: AndroidNotificationOptions(
channelId: 'rali_location_channel',
channelName: 'Rali Location Service',
channelDescription: 'Maintains location updates for navigation',
channelImportance: NotificationChannelImportance.HIGH,
priority: NotificationPriority.HIGH,
iconData: const NotificationIconData(
resType: ResourceType.mipmap,
resPrefix: ResourcePrefix.ic,
name: 'launcher',
),
buttons: [
const NotificationButton(id: 'stopNavigation', text: 'Stop Navigation'),
],
),
iosNotificationOptions: const IOSNotificationOptions(
showNotification: true,
playSound: false,
),
foregroundTaskOptions: const ForegroundTaskOptions(
interval: 1000, // Update interval in milliseconds (1 second)
isOnceEvent: false,
autoRunOnBoot: false,
allowWifiLock: true,
allowWakeLock: true,
),
);
// Initialize the receive port to receive data from the foreground service
_receivePort = FlutterForegroundTask.receivePort;
_receivePort?.listen((message) {
if (message is Position) {
// Convert geolocator Position to our RaliPosition
final raliPosition = RaliPosition(message.longitude, message.latitude);
if (DebugSettings.showLocationLogs) {
debugPrint("[DEBUG] Received location update from foreground service: $raliPosition at ${DateTime.now()}");
}
// Add the position to our stream for listeners
_locationStreamController.add(raliPosition);
} else if (message is String) {
if (message == 'onNotificationButtonPressed.stopNavigation') {
debugPrint("[DEBUG] Stop navigation button pressed at ${DateTime.now()}");
// This would be handled by the app to stop navigation
}
debugPrint("[DEBUG] Received message from foreground service: $message at ${DateTime.now()}");
}
});
}