start static method
Start foreground task with notification.
Implementation
static Future<ReceivePort?> start({
required String notificationTitle,
required String notificationText,
Function? callback,
}) async {
if (await isRunningTask)
throw ForegroundTaskException(
'Already started. Please call this function after calling the stop function.');
if (_foregroundTaskOptions == null)
throw ForegroundTaskException(
'Not initialized. Please call this function after calling the init function.');
final receivePort = _registerPort();
if (receivePort == null)
throw ForegroundTaskException(
'Failed to register SendPort to communicate with background isolate.');
final options = Platform.isAndroid
? _androidNotificationOptions?.toJson() ?? Map<String, dynamic>()
: _iosNotificationOptions?.toJson() ?? Map<String, dynamic>();
options['notificationContentTitle'] = notificationTitle;
options['notificationContentText'] = notificationText;
if (callback != null) {
options.addAll(
_foregroundTaskOptions?.toJson() ?? Map<String, dynamic>());
options['callbackHandle'] =
PluginUtilities.getCallbackHandle(callback)?.toRawHandle();
}
_methodChannel.invokeMethod('startForegroundService', options);
_printMessage('FlutterForegroundTask started.');
return receivePort;
}