ekasetiawans / flutter_background_service

258 stars 171 forks source link

Always showing Preparing Notification #418

Open md-rifatkhan opened 4 months ago

md-rifatkhan commented 4 months ago

Future<void> initializeService() async {
  final service = FlutterBackgroundService();

  const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'my_foreground', // id
    'MY FOREGROUND SERVICE', // title
    description:
    'This channel is used for important notifications.', // description
    importance: Importance.low, // importance must be at low or higher level
  );

  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
  FlutterLocalNotificationsPlugin();

  if (Platform.isIOS || Platform.isAndroid) {
    await flutterLocalNotificationsPlugin.initialize(
      const InitializationSettings(
        iOS: DarwinInitializationSettings(),
        android: AndroidInitializationSettings('ic_bg_service_small'),
      ),
    );
  }

  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
      AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);

  await service.configure(
    androidConfiguration: AndroidConfiguration(
      // this will be executed when app is in foreground or background in separated isolate
      onStart: onStart,

      // auto start service
      autoStart: true,
      isForegroundMode: true,

      notificationChannelId: 'my_foreground',
      initialNotificationTitle: 'Android Service',
      initialNotificationContent: 'Android Service Preparing',
      foregroundServiceNotificationId: 888,

    ),
    iosConfiguration: IosConfiguration(
      // auto start service
      autoStart: true,

      // this will be executed when app is in foreground in separated isolate
      onForeground: onStart,

      // you have to enable background fetch capability on xcode project
      onBackground: onIosBackground,
    ),
  );
}

@pragma('vm:entry-point')
Future<bool> onIosBackground(ServiceInstance service) async {
  WidgetsFlutterBinding.ensureInitialized();
  DartPluginRegistrant.ensureInitialized();

  return true;
}

@pragma('vm:entry-point')
void onStart(ServiceInstance service) async {
  NotificationService().initNotification();
  DartPluginRegistrant.ensureInitialized();

  tz.initializeTimeZones();
  await GetStorage.init();

  NotificationService().showNotification(1, "Fajr Prayer Reminder : ", "It\'s time for Fajr prayer! (Estimated)", "8:58 PM");

  // Notification for Fajr
  String fajrTime = GetStorage().read('fajr') ?? "";
  if (fajrTime.isNotEmpty) {
    NotificationService().showNotification(2, "Fajr Prayer Reminder : $fajrTime", "It\'s time for Fajr prayer! (Estimated)", fajrTime);
  }
  // Notification for Dhuhr
  String dhuhrTime = GetStorage().read('dhuhr') ?? "";
  if (dhuhrTime.isNotEmpty) {
    NotificationService().showNotification(3, "Dhuhr Prayer Reminder : $dhuhrTime", "It\'s time for Dhuhr prayer! (Estimated)", dhuhrTime);
  }

  // Notification for Asr
  String asrTime = GetStorage().read('asr') ?? "";
  if (asrTime.isNotEmpty) {
    NotificationService().showNotification(4, "Asr Prayer Reminder : $asrTime", "It\'s time for Asr prayer! (Estimated)", asrTime);
  }

  // Notification for Maghrib
  String magribTime = GetStorage().read('magrib') ?? "";
  if (magribTime.isNotEmpty) {
    NotificationService().showNotification(5, "Maghrib Prayer Reminder : $magribTime", "It\'s time for Magrib prayer! (Estimated)", magribTime);
  }

  // Notification for Isha
  String ishaTime = GetStorage().read('isha') ?? "";
  if (ishaTime.isNotEmpty) {
    NotificationService().showNotification(6, "Isha Prayer Reminder : $ishaTime", "It\'s time for Isha prayer! (Estimated)", ishaTime);
  }

  String tahajjud = GetStorage().read('lastThirdOfTheNight') ?? "";
  if (ishaTime.isNotEmpty) {
    NotificationService().showNotification(7, "Tahajjur Prayer Reminder : $tahajjud", "It\'s time for Tahajjud! (Estimated)", tahajjud);
  }

  String ishraq = GetStorage().read('ishraq') ?? "";
  if (ishraq.isNotEmpty) {
    NotificationService().showNotification(8, "ishraq Prayer Reminder : $ishraq", "It\'s time for ishraq! (Estimated)", ishraq);
  }

  if (service is AndroidServiceInstance) {
    service.on('setAsForeground').listen((event) {
      service.setAsForegroundService();
    });

    service.on('setAsBackground').listen((event) {
      service.setAsBackgroundService();
    });
  }

  service.on('stopService').listen((event) {
    service.stopSelf();
  });

  // bring to foreground
  /*  Timer.periodic(const Duration(seconds: 1), (timer) async {
    if (service is AndroidServiceInstance) {
      if (await service.isForegroundService()) {
        flutterLocalNotificationsPlugin.show(
          888,
          'COOL SERVICE',
          'Awesome ${DateTime.now()}',
          const NotificationDetails(
            android: AndroidNotificationDetails(
              'my_foreground',
              'MY FOREGROUND SERVICE',
              icon: 'ic_bg_service_small',
              ongoing: true,
            ),
          ),
        );

        service.setForegroundNotificationInfo(
          title: "My App Service",
          content: "Updated at ${DateTime.now()}",
        );
      }
    }

    print('FLUTTER BACKGROUND SERVICE: ${DateTime.now()}');
  });*/
}
md-rifatkhan commented 4 months ago

any way to hide it?