flutter_background_fetch icon indicating copy to clipboard operation
flutter_background_fetch copied to clipboard

[BUG]not working when app is terminated on android

Open johnzhu123 opened this issue 2 years ago • 1 comments

Your Environment: android

  • Plugin version: ^1.2.1
  • Platform: Android
  • OS version: EMUI 8.0.0.561 (based on android)
  • Device manufacturer / model: huawei
  • Flutter info (flutter info, flutter doctor):
[✓] Flutter (Channel beta, 3.17.0-0.0.pre, on macOS 14.1.1 23B81 darwin-arm64, locale en-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 15.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.2)
[✓] IntelliJ IDEA Ultimate Edition (version 2023.1.1)
[✓] VS Code (version 1.85.0)
[✓] Connected device (4 available)
[!] Network resources                   
    ✗ A network error occurred while checking "https://maven.google.com/": Operation timed out
! Doctor found issues in 1 category.
  • Plugin config
// [Android-only] This "Headless Task" is run when the Android app is terminated with `enableHeadless: true`
// Be sure to annotate your callback function to avoid issues in release mode on Flutter >= 3.3.0
@pragma('vm:entry-point')
void backgroundFetchHeadlessTask(HeadlessTask task) async {
  String taskId = task.taskId;
  bool isTimeout = task.timeout;
  if (isTimeout) {
    // This task has exceeded its allowed running-time.
    // You must stop what you're doing and immediately .finish(taskId)
    print("[BackgroundFetch] Headless task timed-out: $taskId");
    // notifyTimeOut();
    BackgroundFetch.finish(taskId);
    return;
  }
  checkBlockedBatch();
  print('[BackgroundFetch] Headless event received.');
  // Do your work here...
  BackgroundFetch.finish(taskId);
}
...

BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
 runApp(MyApp());
 
 ...
 
 void initState() {
    super.initState();
    initPlatformState();
  }

 // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    if (Platform.isAndroid) {
      // Configure BackgroundFetch.
      await BackgroundFetch.configure(
          BackgroundFetchConfig(
              minimumFetchInterval: 15,
              startOnBoot: true,
              stopOnTerminate: false,
              enableHeadless: true,
              requiresBatteryNotLow: false,
              requiresCharging: false,
              requiresStorageNotLow: false,
              requiresDeviceIdle: false,
              requiredNetworkType: NetworkType.NONE), (String taskId) async {
        // <-- Event handler
        // This is the fetch-event callback.
        print("[BackgroundFetch] Event received $taskId");
        checkBlockedBatch();
        // IMPORTANT:  You must signal completion of your task or the OS can punish your app
        // for taking too long in the background.
        BackgroundFetch.finish(taskId);
      }, (String taskId) async {
        // <-- Task timeout handler.
        // This task has exceeded its allowed running-time.  You must stop what you're doing and immediately .finish(taskId)
        // notifyTimeOut();
        print("[BackgroundFetch] TASK TIMEOUT taskId: $taskId");
        BackgroundFetch.finish(taskId);
      });
    } else if (Platform.isIOS) {
      BackgroundFetch.configure(BackgroundFetchConfig(minimumFetchInterval: 15),
          (String taskId) async {
        // <-- Event callback.
        // This is the fetch-event callback.
        print("[BackgroundFetch] taskId: $taskId");
        // Use a switch statement to route task-handling.
        // switch (taskId) {
        //   case 'com.transistorsoft.customtask':
        //     print("Received custom task");
        //     break;
        //   default:
        //     print("Default fetch task");
        // }
        checkBlockedBatch();
        BackgroundFetch.finish(
            taskId); // <-- Disable .finish(taskId) when simulating an iOS task timeout
      }, (String taskId) async {
        // <-- Event timeout callback
        // This task has exceeded its allowed running-time.  You must stop what you're doing and immediately .finish(taskId)
        // notifyTimeOut();
        print("[BackgroundFetch] TIMEOUT taskId: $taskId");
        BackgroundFetch.finish(taskId);
      });
    }

checkBlockedBatch is going to notify issues

To Reproduce Steps to reproduce the behavior:

  1. when app is on background ,everything is fine I can get notification every 15 minutes, but when app is terminated , it not work

johnzhu123 avatar Dec 13 '23 11:12 johnzhu123

See https://dontkillmyapp.com

christocracy avatar Dec 13 '23 12:12 christocracy

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Apr 17 '24 01:04 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar May 01 '24 01:05 github-actions[bot]