Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]not working when app is terminated on android #355

Closed
johnzhu123 opened this issue Dec 13, 2023 · 3 comments
Closed

[BUG]not working when app is terminated on android #355

johnzhu123 opened this issue Dec 13, 2023 · 3 comments
Labels

Comments

@johnzhu123
Copy link

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
@christocracy
Copy link
Member

See https://dontkillmyapp.com

Copy link

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

@github-actions github-actions bot added the stale label Apr 17, 2024
Copy link

github-actions bot commented May 1, 2024

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

@github-actions github-actions bot closed this as completed May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants