onStart function
void
onStart(
- ServiceInstance serviceInstance
Implementation
@pragma('vm:entry-point')
void onStart(ServiceInstance serviceInstance) async {
print("in onStart");
DartPluginRegistrant.ensureInitialized();
if (serviceInstance is AndroidServiceInstance) {
// Android-specific logic
print("in onStart setAsForeground ");
serviceInstance.on('setAsForeground').listen((event) {
serviceInstance.setAsForegroundService();
});
serviceInstance.on('setAsBackground').listen((event) {
print("in onStart setAsBackground ");
serviceInstance.setAsBackgroundService();
});
serviceInstance.on('stopService').listen((event) {
print("in onStart stopService ");
serviceInstance.stopSelf();
});
Timer.periodic(const Duration(seconds: 10), (timer) async {
if (await serviceInstance.isForegroundService()) {
String? currentUsername = await MessageService().getUsername();
print("in Timer.periodic ==========>$currentUsername ");
if (currentUsername != null && currentUsername.isNotEmpty) {
await MessageService().checkForMessages(currentUsername);
} else {
debugPrint(
EnC.val('Q3VycmVudCB1c2VybmFtZSBpcyBudWxsIG9yIGVtcHR5Lg=='),
);
}
}
});
} else {
// iOS-specific logic
print("in else iOS background fetch triggered");
Timer.periodic(const Duration(seconds: 10), (timer) async {
String? currentUsername = await MessageService().getUsername();
print("in Timer.periodic ==========>$currentUsername ");
if (currentUsername != null && currentUsername.isNotEmpty) {
await MessageService().checkForMessages(currentUsername);
} else {
debugPrint(
EnC.val('Q3VycmVudCB1c2VybmFtZSBpcyBudWxsIG9yIGVtcHR5Lg=='),
);
}
});
serviceInstance.invoke('finish');
}
}