How to run tasks in workmanager?

20 Views Asked by At

I have a WorkManager service handling task and the main task is about sending notifications at specific times, I need after the task to complete executing the function but It has to be done after the notification is sent.

My WorkManager class is

class WorkServiceManager {
  late String id;
  void init() {
    Map<String, dynamic>? notificationDetails =
        Storage().box.read("notification_details");

    id = notificationDetails!['id'].toString();
    //final ReminderController reminderController = Get.find();
    Workmanager().initialize(
        actionTask, // The top level function, aka callbackDispatcher
        isInDebugMode:
            false // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
        );
    Workmanager().registerPeriodicTask(
      id, "Show Notification_$id",
      inputData: notificationDetails,
      //initialDelay: const Duration(seconds: 15),
    );
    log("task registered with ID $id");
  }
}

@pragma('vm:entry-point')
void actionTask() {
  Workmanager().executeTask((taskName, inputData) async {
    log(inputData?['name']);
    await LocalNotificationServices.showSchedualNotification(
      inputData?['id'],
      inputData?['name'],
      inputData?['describtion'],
      inputData?['reminderdate'],
      inputData?['remindertime'],
    );
    await performDatabaseUpdate(taskName);
    return Future.value(true);
  });
}

Future<void> performDatabaseUpdate(String tasknames) async {
  // Your database update logic goes here
  String s = tasknames.split("_")[1];
  await Get.put(ReminderController()).updateReminderStatus(int.parse(s), false);
  await Workmanager().cancelByUniqueName(s);
  log("Database Updated");
}
1

There are 1 best solutions below

0
Emad Younan On

I found another package that will do what I need to do

android_alarm_manager_plus

thanks everyone