Skip to content

Foreground task Started but do nothing #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
GaelleJoubert opened this issue Nov 16, 2021 · 3 comments
Closed

Foreground task Started but do nothing #26

GaelleJoubert opened this issue Nov 16, 2021 · 3 comments

Comments

@GaelleJoubert
Copy link
Contributor

I'm having an issue with my Foreground task (on IOS and Android).
I know it starts :

  • I have a log message : "FlutterForegroundTask started"
  • I have the notification that pop up on the phone screen.

But it doesn't seems to do anything I've put in the on start or on event function.
I've put "print" and "log", in the "on start" function, it should be printed on my run console, or is there something I'm misunderstanding ?
Also about the "on event" function, it should be called periodically with the interval specified in the init function, right ?
(I want to be sure that I understood correctly).

Here is my code :
The init function :

FlutterForegroundTask.init(
      androidNotificationOptions: AndroidNotificationOptions(
        channelId: 'notification_channel_id',
        channelName: 'Foreground Notification',
        channelDescription: 'This notification appears when the foreground service is running.',
        channelImportance: NotificationChannelImportance.LOW,
        priority: NotificationPriority.LOW,
        iconData: NotificationIconData(
          resType: ResourceType.mipmap,
          resPrefix: ResourcePrefix.ic,
          name: 'launcher',
        ),
      ),
      iosNotificationOptions: IOSNotificationOptions(
        showNotification: true,
        playSound: false,
      ),
      foregroundTaskOptions: ForegroundTaskOptions(
        interval: 5000,
        autoRunOnBoot: true,
      ),
      printDevLog: true,
    );

The function called to start the background service :

void startBackground2(BuildContext? context) async {
  ReceivePort? _receivePort;
  await FlutterForegroundTask.saveData(key: 'customData', value: 'hello');
  print("Test1");
  _receivePort = await FlutterForegroundTask.startService(
    notificationTitle: 'Foreground Service is running',
    notificationText: 'Tap to return to the app',
    callback: () {startCallback();
    },
  );
  _receivePort?.listen((message) {
    if (message is DateTime)
      print('receive timestamp: $message');
  });
}

The start call back and the TaskHandler :

void startCallback() {
  // The setTaskHandler function must be called to handle the task in the background.
  FlutterForegroundTask.setTaskHandler(FirstTaskHandler());
}

class FirstTaskHandler implements TaskHandler {
  @override
  Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
    print("allez la");
    log("log la");
    final customData = await FlutterForegroundTask.getData<String>(key: 'customData');
    print('customData: ');
    print('log: ');


  }

  @override
  Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {
    // Send data to the main isolate.
    print('yeah !');
    log('log yeah ');
    sendPort?.send(timestamp);
  }

  @override
  Future<void> onDestroy(DateTime timestamp) async {
    // You can use the clearAllData function to clear all the stored data.
    await FlutterForegroundTask.clearAllData();
  }
}

As I said before, The service is starting, but I have Zero prints or log which are working, and even the print on " _receivePort?.listen((message) " doesn't work.

Would you have any idea why, or what did I not understand correctly ?
Thanks a lot !

@Dev-hwang
Copy link
Owner

You need to put a top-level function in the callback parameter.

Run the code below:

_receivePort = await FlutterForegroundTask.startService(
    notificationTitle: 'Foreground Service is running',
    notificationText: 'Tap to return to the app',
    callback: startCallback,  // fix here
);

@Dev-hwang
Copy link
Owner

_receivePort = await FlutterForegroundTask.startService(
    notificationTitle: 'Foreground Service is running',
    notificationText: 'Tap to return to the app',
    callback: () {
       // Creating a callback in this way is not a top-level function.
    }
);

@GaelleJoubert
Copy link
Contributor Author

I applied your fix and it works now !! Thanks :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants