-
Notifications
You must be signed in to change notification settings - Fork 124
Send data from main isolate to the foreground service #22
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
Comments
This plugin has no way to pass any more data once the foreground service is started. However, if you have not started the service, you can use data management functions to send data to the TaskHandler. See the example below. In this example, the // The callback function should always be a top-level function.
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 {
// You can use the getData function to get the data you saved.
final customData = await FlutterForegroundTask.getData<String>(key: 'customData');
print('customData: $customData');
}
@override
Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {
// Send data to the main isolate.
sendPort?.send(timestamp);
}
@override
Future<void> onDestroy(DateTime timestamp) async {
}
}
class ExampleApp extends StatefulWidget {
@override
_ExampleAppState createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
ReceivePort? _receivePort;
// ...
void _startForegroundTask() async {
// You can save data using the saveData function.
await FlutterForegroundTask.saveData(key: 'customData', value: 'hello');
if (await FlutterForegroundTask.isRunningService) {
_receivePort = await FlutterForegroundTask.restartService();
} else {
_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');
});
}
@override
void dispose() {
_receivePort?.close();
super.dispose();
}
} The data management functions supported by this plugin are as follows. void function() async {
await FlutterForegroundTask.getData(key: String);
await FlutterForegroundTask.saveData(key: String, value: Object);
await FlutterForegroundTask.removeData(key: String);
await FlutterForegroundTask.clearAllData();
} |
As title says, I am trying to send some settings from main isolate to the foreground service but i couldn't find any tutorial about how to achieve this
The text was updated successfully, but these errors were encountered: