How to change the callback interval of foreground service?
Is there any way to change the interval of the callback if a certain requirement is met during the callback? I tried calling the init function with a different interval and restarting the service, but it doesn't change from the initial interval.
I didn't really change many parts of the sample code, but here it is:
Future<void> initializeForegroundTask(int interval) async {
T? _ambiguate<T>(T? value) => value;
int intervalValue = interval;
GeneralFunctions.initForegroundTask(intervalValue).then((value) {
_ambiguate(WidgetsBinding.instance)?.addPostFrameCallback((_) async {
// You can get the previous ReceivePort without restarting the service.
if (await FlutterForegroundTask.isRunningService) {
final newReceivePort = await FlutterForegroundTask.receivePort;
GeneralFunctions.registerReceivePort(newReceivePort);
}
});
startForegroundTask();
});
}
static Future<void> initForegroundTask(int interval) async {
await FlutterForegroundTask.init(
androidNotificationOptions: GeneralVariables.androidNotificationOptions,
iosNotificationOptions: GeneralVariables.iosNotificationOptions,
foregroundTaskOptions: ForegroundTaskOptions(
interval: interval,
autoRunOnBoot: true,
allowWifiLock: true,
),
printDevLog: true,
);
}
static Future<bool> startForegroundTask() async {
if (!await FlutterForegroundTask.canDrawOverlays) {
final isGranted =
await FlutterForegroundTask.openSystemAlertWindowSettings();
if (!isGranted) {
print('SYSTEM_ALERT_WINDOW permission denied!');
return false;
}
}
ReceivePort? receivePort;
if (await FlutterForegroundTask.isRunningService) {
print("RESTARTING");
receivePort = await FlutterForegroundTask.restartService();
} else {
print("STARTING");
receivePort = await FlutterForegroundTask.startService(
notificationTitle: 'Title',
notificationText: 'Text',
callback: startCallback,
);
}
return GeneralFunctions.registerReceivePort(receivePort);
}
And here's the code for the handler and callback function:
void startCallback() {
FlutterForegroundTask.setTaskHandler(MyTaskHandler());
}
class MyTaskHandler extends TaskHandler {
SendPort? _sendPort;
@override
Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
_sendPort = sendPort;
}
@override
Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {
FlutterForegroundTask.updateService(
notificationTitle: 'Updated Title',
notificationText: 'Updated Text');
await GeneralFunctions.initializeForegroundTask(20000);
}
@override
Future<void> onDestroy(DateTime timestamp, SendPort? sendPort) async {
await FlutterForegroundTask.clearAllData();
}
@override
void onButtonPressed(String id) {
print('onButtonPressed >> $id');
}
@override
void onNotificationPressed() {
_sendPort?.send('onNotificationPressed');
}
}
Hello. I have the same question.
I tried several things, but no one works: In _receivePort listening function that handles messages from onEvent::_sendPort?.send(message):
- I tried to _closeReceivePort(), then _stopForegroundTask(), then _initForegroundTask with a different interval, and finally _startForegroundTask
- Yhe same without _closeReceivePort
- I tried to only _initForegroundTask again with another interval and _startForegroundTask (so that it restarts)
- I also tried 1, 2 and 3 in a Timer callbcak function called after the _receivePort listening function has finished (mutex protection)
For 3: the foreground task still work, but at the same interval. For 1: I don't really understand, it seems the foreground task still runs, but my _receivePort listening function is not called anymore.
What would you advise to do change the OnEvent interval ?
Thanks a lot for your work.
Hi @joeldunamis . Did you solve this problem ? Or did you workaround ?
Upgrade to 6.0.0
You can change the interval using foregroundTaskOptions in FlutterForegroundTask.updateService function.
@override
Future<void> onRepeatEvent(DateTime timestamp, SendPort? sendPort) async {
if (_eventCount == 10) {
FlutterForegroundTask.updateService(
foregroundTaskOptions: const ForegroundTaskOptions(interval: 1000),
);
} else {
_eventCount++;
}
}
Hi @Dev-hwang Wonderful, thanks a lot. I'll try.