import 'package:flutter_foreground_task/flutter_foreground_task.dart';

Future<ServiceRequestResult> startForegroundService({
  int? serviceId,
  required String notificationTitle,
  required String notificationText,
  NotificationIconData? notificationIcon,
  List<NotificationButton>? notificationButtons,
  required Function callback,
}) async {
  final NotificationPermission notificationPermission =
      await FlutterForegroundTask.checkNotificationPermission();
  if (notificationPermission != NotificationPermission.granted) {
    await FlutterForegroundTask.requestNotificationPermission();
  }
  if (await FlutterForegroundTask.isRunningService) {
    return FlutterForegroundTask.restartService();
  } else {
    return FlutterForegroundTask.startService(
      serviceId: serviceId,
      notificationTitle: notificationTitle,
      notificationText: notificationText,
      notificationIcon: notificationIcon,
      notificationButtons: notificationButtons,
      callback: callback,
    );
  }
}

Future<void> stopForegroundService() async {
  if (await FlutterForegroundTask.isRunningService) {
    FlutterForegroundTask.stopService();
  }
}
