JulianAssmann / flutter_background

A flutter plugin to keep apps running in the background via foreground services. Android only.
https://pub.dev/packages/flutter_background
MIT License
89 stars 52 forks source link

[BUG] Doesn't work in real devices #57

Open Baldur008 opened 2 years ago

Baldur008 commented 2 years ago

Describe the bug It works in emulator but not in a real device

To Reproduce

FlutterBackground.initialize(
        androidConfig: FlutterBackgroundAndroidConfig(
          notificationTitle: "Running in Background",
          notificationImportance: AndroidNotificationImportance.Max,
          notificationText: "Your Location is being updated in background",
        ),
      )
FlutterBackground.enableBackgroundExecution();

Expected behavior I am calling both initialize and enablebackgroundexecution at the same time. It works on emulator but not in a real device

Smartphone:

Logs:

No Exception/Error Found.

Sesu8642 commented 2 years ago

enableBackgroundExecution requires the initialization to be finished. In the emulator, the initialization is presumably fast enough to be finished by chance. You should probably use await:

await FlutterBackground.initialize();
await FlutterBackground.enableBackgroundExecution();

If you need to do it in a non-async function, you can also do the following:

FlutterBackground.initialize().then((success) => FlutterBackground.enableBackgroundExecution());
omerhertz commented 2 years ago

Same issue for me. Working fine with emulator. I see that the await are done, but the app code is not running I am using:

const androidConfig = FlutterBackgroundAndroidConfig(
    notificationTitle: "App name",
    notificationText: "App is storing your location even when running in background",
    notificationImportance: AndroidNotificationImportance.Max,
    notificationIcon: AndroidResource(name: 'background_icon', defType: 'drawable'),
  );
  await FlutterBackground.initialize(androidConfig: androidConfig);
  await FlutterBackground.enableBackgroundExecution();
omerhertz commented 2 years ago

The problem was in my real device configuration! I allowed my app to use location all the time, and it started working in the background too!

JavesonYehudi commented 2 years ago

There's definitely an issue on real devices. You have to initialise the configuration twice in order for it to work.

innerk commented 2 years ago

same happened to me, on emulator works but on real devices throws

java.lang.RuntimeException: Unable to start service de.julianassmann.flutter_background.IsolateHolderService@8ca558 with Intent { act=START cmp=com.imvaswms.invaswmsdm/de.julianassmann.flutter_background.IsolateHolderService }: java.lang.IllegalArgumentException

nawbc commented 1 year ago

Unable to start service Intent { act=START cmp=com.deskbtm.nitmgpt/de.julianassmann.flutter_background.IsolateHolderService } U=0: not found