I'm trying to make reminders with notifications on my app so I have a reminder views screen that I'm getting dependence injection by
final ReminderController reminderController = Get.find();
I'm creating a reminder notification and the default is active so I've made the work manager class.
class WorkServiceManager {
late String id;
void init() {
id = Storage().box.read("reminder_id");
//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().registerOneOffTask(
id,
"Show Notification_$id",
initialDelay: const Duration(seconds: 15),
);
log("task registered with ID $id");
}
void cancelTask(String id) {
Workmanager().cancelByUniqueName(id);
}
}
@pragma('vm:entry-point')
void actionTask() {
Workmanager().executeTask((taskName, inputData) async {
LocalNotificationServices.showBasicNotification(
0,
"Sch Notification",
"Body Body",
);
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);
log("Database Updated");
}
to run schedule notification and after the notification is done I run an update script into the database to make it off like attaching an image
and that's my UI screen
SizedBox(
child: Obx(() {
if (reminderController.reminders.isEmpty) {
return const Center(child: null);
} else {
return ListView.builder(
itemCount: reminderController.reminders.length,
shrinkWrap: true,
itemBuilder: (context, index) {
Reminder remind = reminderController.reminders[index];
return ListTile(
title: Text(remind.name!),
subtitle: Text(remind.describtion!),
trailing: Switch(
value: remind.reminderactive!,
onChanged: (bool newval) async {
await reminderController.updateReminderStatus(
remind.id!,
newval,
);
},
),
);
},
);
}
}),
and that's my getx controller
class ReminderController extends GetxController {
var reminders = <Reminder>[].obs;
@override
void onInit() {
super.onInit();
fetchReminders();
}
Future<void> fetchReminders() async {
try {
reminders.value = await DatabaseHelper.allreminders();
} catch (e) {
// Handle errors, if any
log("Error fetching reminders: $e");
}
}
Future<void> updateReminderStatus(int id, bool newStatus) async {
try {
// Update reminder status in the database
await DatabaseHelper.updateReminderStatus(id, newStatus);
// Refresh reminders list
await fetchReminders();
} catch (e) {
// Handle errors, if any
log("Error updating reminder status: $e");
}
}
}
I need the user on the UI screen and the notification done when he is online the work manager updates that switch from true to false I hope I wrote my question clearly and thanks all in advance.
