I use awesome_notifications package for scheduling local notifications in my todo app.
I tried to find the solution here and on other sources too, even in the package documentation, but I couldn't find.
I want the following:
"When the app is in foreground and active, which is SchedulerBinding.instance.lifecycleState == resumed in flutter, then the notification action button press should trigger UI rebuild. For example, user presses task finished action button in notification tray and the app is currently active, then this task's checkbox should immediately get checked and it should show it immediately, I tried to do the following in awesome_notification's onActionReceived method
@pragma("vm:entry-point")
Future<void> onActionReceivedMethod(ReceivedAction receivedAction) async {
try {
Task task = TaskUtilities.fromNotificationPayload(receivedAction.payload ?? {});
final appState = SchedulerBinding.instance.lifecycleState;
//Prints resumed when app is open
debugPrint('App state right now: $appState');
if (receivedAction.buttonKeyPressed == 'FINISHED') {
if (appState == AppLifecycleState.resumed) {
//Using GetIt package to access TaskViewModel which is a ChangeNotifier, without
//BuildContext.The following line is working correctly, just that it is not
//triggering immediate UI rebuild, I have to perform a hot restart, and when I do
//hot restart, the checkbox is showing checked. toggleStatus() method calls
//notifyListeners().
getIt<TaskViewModel>().toggleStatus(task, true, DateTime.now());
} else {
if (task.isRepeating) {
final cbox = ObjectBox.store.box<TaskCompletion>();
final now = DateTime.now();
final completion = TaskCompletion(date: now, isDone: true);
completion.task.target = task;
cbox.put(completion);
} else {
task.isDone = true;
ObjectBox.store.box<Task>().put(task);
}
}
} else {
MinimalTodo.navigatorKey.currentState
?.push(MaterialPageRoute(builder: (_) => AddTaskPage(edit: true, task: task)));
}
}catch (e, t){
MiniLogger.e('Error was thrown by onActionReceivedMethod: ${e.toString()}\nStacktrace: ${t.toString()}\n Error type: ${e.runtimeType}');
}
}
No error is thrown, just the desired action is not getting achieved
Ok so I solved the problem by providing the TaskViewModel instance with getIt like this:
ChangeNotifierProvider(create: (_) => getIt<TaskViewModel>())