I want to fetch data from native side to flutter in background service call. I have implemented flutter_background_service to work with foreground and background tasks at flutter side.
@pragma('vm:entry-point')
void onStart(ServiceInstance service) async {
if (service is AndroidServiceInstance) {
service.on('setAsForeground').listen((event) {
service.setAsForegroundService();
});
service.on('setAsBackground').listen((event) {
service.setAsBackgroundService();
});
}service.on('stopService').listen((event) {
service.stopSelf();
});
Timer.periodic(const Duration(seconds: 5), (timer) async {
if (service is AndroidServiceInstance) {
if (await service.isForegroundService()) {
print('FLUTTER FOREGROUND SERVICE: ${DateTime.now()}');
WidgetsFlutterBinding.ensureInitialized();
try {
await bgMethodChannel.invokeMethod(bgMethod);
} catch (e) {
if (kDebugMode) {
print("EXCEPTION: $e");
}
}
}
}
});
}
print statement is getting printed in every 5 seconds in foreground but method channel call is throwing exception : MissingPluginException(No implementation found for method bg_method on channel inhalerChannelBG)
await bgMethodChannel.invokeMethod(bgMethod);
this line is working everywhere else but not inside onStart. I have used it calling in main() , that gives me results from android native side.
Can anyone help me out?