Skip to content

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

Closed
a-ahmed116 opened this issue Nov 10, 2021 · 1 comment
Closed

Send data from main isolate to the foreground service #22

a-ahmed116 opened this issue Nov 10, 2021 · 1 comment

Comments

@a-ahmed116
Copy link

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

@Dev-hwang
Copy link
Owner

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 FlutterForegroundTask.saveData function is used to save data, and the FlutterForegroundTask.getData function is used to get data from the TaskHandler.

// 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();
}

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