ekasetiawans / flutter_background_service

269 stars 192 forks source link

Background service not working automatically in ios #220

Open kkjk1176 opened 2 years ago

kkjk1176 commented 2 years ago

After building in release mode in Xcode, I looked at the log. When the app is placed in the background by raising the indicator at the bottom, the background service does not work. When I manually click background import in xcode, the service works fine, but after waiting for 30 minutes the service doesn't work again. As far as I know, ios knows that the service should restart every 15 minutes.

In addition, in the case of Android, the following logs are continuously exposed. D/BackgroundService( 8070): Service already running, using existing service

main.dart

void main() async {
  FlutterNativeSplash.preserve(
    widgetsBinding: WidgetsFlutterBinding.ensureInitialized(),
  );
.....

  initMQTT(client);
  await initializeService();
  await initNotificationFilter();

  runApp(
    ProviderScope(
      child: EasyLocalization(
        supportedLocales: const [Locale('en'), Locale('ko')],
        path: 'assets/translations',
        fallbackLocale: const Locale('en'),
        child: const AppMain(),
      ),
    ),
  );
}

Future<void> initializeService() async {
  final service = FlutterBackgroundService();

  await service.configure(
    androidConfiguration: AndroidConfiguration(
      onStart: onStart,
      autoStart: true,
      isForegroundMode: true,
    ),
    iosConfiguration: IosConfiguration(
      autoStart: true,
      onForeground: onStart,
      onBackground: onIosBackground,
    ),
  );

  service.startService();
}

@pragma('vm:entry-point')
bool onIosBackground(ServiceInstance service) {
  WidgetsFlutterBinding.ensureInitialized();
  print('FLUTTER BACKGROUND FETCH!');
  return true;
}

@pragma('vm:entry-point')
Future<void> onStart(ServiceInstance service) async {
  DartPluginRegistrant.ensureInitialized();
  Timer.periodic(const Duration(seconds: 1), (timer) async {
    print(
        '${DateTime.now().minute}:${DateTime.now().second} onStart Running...');
  });
  if (service is androidBackgroundService.AndroidServiceInstance) {
    if (await service.isForegroundService()) {

    }
  }
}

doctor

[✓] Flutter (Channel stable, 3.3.2, on macOS 12.6 21G115 darwin-x64, locale ko-KR)
    • Flutter version 3.3.2 on channel stable at /Users/leedonghee/Documents/libraries.nosync/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision e3c29ec00c (12일 전), 2022-09-14 08:46:55 -0500
    • Engine revision a4ff2c53d8
    • Dart version 2.18.1
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc3)
    • Android SDK at /Users/leedonghee/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0-rc3
    • ANDROID_HOME = /Users/leedonghee/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14A309
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] VS Code (version 1.71.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.48.0

[✓] Connected device (3 available)
    • SM G973N (mobile) • adb-R39M208DM4V-JrnhIC._adb-tls-connect._tcp. • android-arm64  • Android 12 (API 31)
    • macOS (desktop)   • macos                                         • darwin-x64     • macOS 12.6 21G115 darwin-x64
    • Chrome (web)      • chrome                                        • web-javascript • Google Chrome 105.0.5195.125

[✓] HTTP Host Availability
    • All required HTTP hosts are available
ekasetiawans commented 2 years ago

on Android, there is a WatchdogReceiver that executed by AlarmManager to ensure background service is running every 5 seconds.

on iOS there is no long running background service equivalent of Android. On iOS, the onBackground method will be executed by background fetch capability, that will be executed periodically every about 15 minutes once, and will be alive about 20-30 seconds.

kkjk1176 commented 2 years ago

on Android, there is a WatchdogReceiver that executed by AlarmManager to ensure background service is running every 5 seconds.

on iOS there is no long running background service equivalent of Android. On iOS, the onBackground method will be executed by background fetch capability, that will be executed periodically every about 15 minutes once, and will be alive about 20-30 seconds.

I know everything you said about ios. But what I'm talking about is two things.

  1. Background fetch does not work when the app is placed directly in the background
  2. Periodically (15 minutes) background fetch does not work.
ekasetiawans commented 2 years ago

on Android, there is a WatchdogReceiver that executed by AlarmManager to ensure background service is running every 5 seconds. on iOS there is no long running background service equivalent of Android. On iOS, the onBackground method will be executed by background fetch capability, that will be executed periodically every about 15 minutes once, and will be alive about 20-30 seconds.

I know everything you said about ios. But what I'm talking about is two things.

  1. Background fetch does not work when the app is placed directly in the background
  2. Periodically (15 minutes) background fetch does not work.

is it happened in example project too?

kkjk1176 commented 2 years ago

on Android, there is a WatchdogReceiver that executed by AlarmManager to ensure background service is running every 5 seconds. on iOS there is no long running background service equivalent of Android. On iOS, the onBackground method will be executed by background fetch capability, that will be executed periodically every about 15 minutes once, and will be alive about 20-30 seconds.

I know everything you said about ios. But what I'm talking about is two things.

  1. Background fetch does not work when the app is placed directly in the background
  2. Periodically (15 minutes) background fetch does not work.

is it happened in example project too?

yes the same problem occurs.

Also, in the example code, when backgroundFetch is executed manually in xcode, the backgroundFetch log is displayed, and the background service runs for only 1 second and ends.

ekasetiawans commented 2 years ago

Got it, the background service on iOS will be terminated immediately after the Future is completed, but it will be force terminated by OS if the Future takes more than 20 - 30 seconds.

I will investigate why the background fetch doesn't running in release mode when I have enough time. Thank you.

ekasetiawans commented 2 years ago

I have updated 2.4.1 to fix this, make sure the background fetch simulation is running from the xcode. In release mode, we can't expect the onBackground method executed exactly in every 15-20 minutes, because it's still up to the OS. See this explanation https://developer.apple.com/videos/play/wwdc2020/10063/

ekasetiawans commented 2 years ago

IMG_0128

Here is the latest example project that showing background fetch log.

darmawan01 commented 1 year ago

In release mode, we can't expect the onBackground method executed exactly in every 15-20 minutes, because it's still up to the OS

So it's mean, we can't know is background service running on release mode ?

I have this issue, displaying notif when debug mode. but after release mode app not displaying notification