flutter_background_service
flutter_background_service copied to clipboard
Service not Work when app killed
Please fix this issue Service not Work when app killed
I want for location updates in terminated state but it is not working in terminated or killed state, please fix this issue
@Saikiran24 there are 2 kinds "terminated" mean. First, terminated by system which is the app terminated because of system need more resources. And second, terminated by user which is the app terminated from App Info (Force Stop).
If your app has terminated by system, the service will automatically respawn after few seconds or minutes. But, you need extra configuration (manually by user) on some device manufacturer with custom android OS e.g MIUI by xiaomi that have custom security app to enable auto start of app, battery optimizations, background lock, etc.
If your app has terminated by user, then the service will never auto started (respawn) until user opening the app.
Clearing app from recent task, will have the same behavior with terminated by system.
If you still facing issue, make sure you have updated the latest package version which is 2.x
Note that, accessing location from the background need extra permission.
@ekasetiawans Yes, actually i am clearing app from recent task so that it is printing only runServiece, but i also want to execute some code like print this "hello flutter" statement again once it is terminated and also giving autostart permission true and battery optimization to no restriction in MIUI by xioami, here is the complete code
await service.configure( androidConfiguration: AndroidConfiguration( // this will executed when app is in foreground or background in separated isolate onStart: onStart, autoStart: true,
isForegroundMode: true,
),
iosConfiguration: IosConfiguration(
// auto start service
autoStart: true,
// this will executed when app is in foreground in separated isolate
onForeground: onStart,
// you have to enable background fetch capability on xcode project
onBackground: onIosBackground,
),
); }
onStart() async { WidgetsFlutterBinding.ensureInitialized();
if (Platform.isIOS) FlutterBackgroundServiceIOS.registerWith(); if (Platform.isAndroid) FlutterBackgroundServiceAndroid.registerWith();
final service = FlutterBackgroundService();
service.setAsForegroundService();
debugPrint("Hi Flutter1");
}
and also can we use this in flutter ,
it is in android, for re running the service when user removes app from recent task.
@Override public void onTaskRemoved(Intent rootIntent) {
if (!isMyserviceRunning(ForegroundService.class, context)) { Intent startIntent = new Intent(context, ForegroundService.class); // ContextCompat.startForegroundService(context,startIntent); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(startIntent); } else { context.startService(startIntent); } } super.onTaskRemoved(rootIntent); }
any updates !!
@Saikiran24 have you tried with the latest version? I'm using xiaomi too, but my app works fine.
This are the versions currently in use flutter_background_service: ^2.0.0 flutter_background_service_ios: ^2.0.0 flutter_background_service_android: ^2.0.0
Here is the code of onStart
void onStart(ServiceInstance service) {
if (service is AndroidServiceInstance) {
service.on('startSocketService').listen((event) {
// service.setAsForegroundService();
service.setAsBackgroundService();
initSocket(event?['id']);
});
}
service.on('stopService').listen((event) {
service.stopSelf();
});
}
and iam invoking method this way
FlutterBackgroundService()
.invoke("startSocketService", {'id': mProfileData?.id.toString()});
@melbanna40 the method service.on will be called only when we call .invoke method from the UI. It will not be called automatically when the service respawn. So, you have to make the logic for respawn.
The service is started and it is working fine in foreground and background but when the app is killed or I remove it from recent apps the service stops I am also using Xiaomi device
@Saikiran24
it is printing only runServiece, but i also want to execute some code like print this "hello flutter"
the log runService
and hello flutter
will be printed with different log tags. So you have to re-check the logs. Did you tried the example project and confirm that the notification is not updated when app was killed?
@melbanna40 it should respawned after few seconds or minutes, unless there are the OS restrictions such as Security App on MIUI, Battery Optimizations and also Background Processing permission. Could you confirm with example project without any modification?
Note that, we have to run in release mode to make sure the binary is stable.
Hi, I have the same issue on iOS with the latest version (2.1.1). I followed the guide:
- Enabled background fetching
- Add in the info.plist:
<key>BGTaskSchedulerPermittedIdentifiers</key> <array> <string>dev.flutter.background.refresh</string> </array>
But when I close my app, the task is not called... I have you an idea?
Regards, Daniel
@dangabdolfo have you tried to add background fetch capability from the xcode ?
To be sure, I test with the example app present in the package. And in the Xcode projet all seem good:
- Background fetch capability is enabled
- The task id is added to the plist.info
And I get the same behavior... the onBackground method is never called