initialize static method
initialize Initializes the background service.
This must be called before any other methods can be used.
The autoStart
parameter specifies whether the service should be started
automatically when the app is launched. If not specified, this defaults to
false
.
The notificationChannelId
, initialNotificationTitle
, and
initialNotificationContent
parameters customize the notification that is
displayed while the service is running. If not specified, these default to
"step_tracker_channel"
, "Step Tracker"
, and "Tracking your steps"
,
respectively.
The foregroundServiceNotificationId
parameter specifies the ID of the
notification that is displayed while the service is running. If not
specified, this defaults to 1
.
Returns true
if the service is initialized successfully, false
otherwise.
Implementation
static Future<bool> initialize(StepLoggerConfig config) async {
try {
await _service.configure(
androidConfiguration: AndroidConfiguration(
onStart: onStart,
autoStart: false,
isForegroundMode: true,
notificationChannelId: 'step_tracker_channel',
initialNotificationTitle:
config.trackingNotificationTitle ?? 'Step Tracker',
initialNotificationContent:
config.trackingNotificationContent ?? 'Tracking your steps',
foregroundServiceNotificationId: 1,
),
iosConfiguration: IosConfiguration(),
);
final prefs = await SharedPreferences.getInstance();
await prefs.setString(
'bg_service_config',
jsonEncode({
'title': config.trackingNotificationTitle,
'content': config.trackingNotificationContent,
}));
return true;
} catch (e) {
return false;
}
}