You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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 !
The text was updated successfully, but these errors were encountered:
You need to put a top-level function in the callback parameter.
Run the code below:
_receivePort =awaitFlutterForegroundTask.startService(
notificationTitle:'Foreground Service is running',
notificationText:'Tap to return to the app',
callback: startCallback, // fix here
);
_receivePort =awaitFlutterForegroundTask.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.
}
);
I'm having an issue with my Foreground task (on IOS and Android).
I know it starts :
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 :
The function called to start the background service :
The start call back and the TaskHandler :
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 !
The text was updated successfully, but these errors were encountered: