flutter_foreground_task icon indicating copy to clipboard operation
flutter_foreground_task copied to clipboard

OnNotificationPressed not activating due to error in example code?

Open fbdesignpro opened this issue 1 year ago • 0 comments

After much experimenting, I've got everything running but couldn't get OnNotificationPressed or onNotificationButtonPressed to work reliably.

Turns out the issue was that the sample code (which I was using) for onRepeatEvent was causing this error at runtime:

E/flutter (15661): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument: is a regular instance reachable via : Instance of 'DateTime'
E/flutter (15661): #0      _SendPort._sendInternal (dart:isolate-patch/isolate_patch.dart:250:43)
E/flutter (15661): #1      _SendPort.send (dart:isolate-patch/isolate_patch.dart:231:5)
E/flutter (15661): #2      FirstTaskHandler.onRepeatEvent (package:flutter_application_1/widgets/test_page.dart:39:15)
E/flutter (15661): #3      FlutterForegroundTask.setTaskHandler.<anonymous closure> (package:flutter_foreground_task/flutter_foreground_task.dart:269:19)

I haven't investigated the error too much and this could be the side effect of something else, but thought I would post it here in case anyone else runs into this (everything running but the events not firing). The error text only shows up when "continuing" the debugger (as it seems to disconnect when the application loses focus).

I just commented out that line (the .send() call) and everything works perfectly:

  // Called every [interval] milliseconds in [ForegroundTaskOptions].
  @override
  void onRepeatEvent(DateTime timestamp, SendPort? sendPort) async {
    // Send data to the main isolate.
    // sendPort?.send(timestamp);
  }

Again, this might be my fault or due to some misconfiguration but I would be curious if this makes sense for someone out there, or at least can be helpful.

fbdesignpro avatar Feb 03 '24 02:02 fbdesignpro