Dev-hwang / flutter_foreground_task

This plugin is used to implement a foreground service on the Android platform.
https://pub.dev/packages/flutter_foreground_task
MIT License
140 stars 105 forks source link

OnNotificationPressed not activating due to error in example code? #199

Closed fbdesignpro closed 1 month ago

fbdesignpro commented 7 months ago

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.

Dev-hwang commented 1 month ago

SendPort doesn't seem to allow sending DateTime in release mode.

Updated readme on how to send DateTime.

Thank you for reporting the issue.