0

Hi I want my android app work in background even if the program completely close(terminate). I use the flutter_background_fetch for it. the notification show every 15 min correctly in app is open or go to background, But when I terminate app the background_fetch not work and not show the notification or do anything anymore. here this my main file config:

void main() async {

  WidgetsFlutterBinding.ensureInitialized();
  BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
  initializeNotificationService();
  runApp(const MyApp());
}


@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)
    BackgroundFetch.finish(taskId);
    return;
  }
  // Do your work here...
  BackgroundFetch.finish(taskId);
}

and this is part of my controller body for background_fetch:

class Controller extends GetxController {
  
  @override
  void onInit() {
    // TODO: implement onInit

    super.onInit();
    startListeningNotificationEvents();
    initPlatformState();
  }
  
  Future<void> initPlatformState() async {
    var status = await BackgroundFetch.configure(
        BackgroundFetchConfig(
          minimumFetchInterval: 15,
          forceAlarmManager: false,
          stopOnTerminate: false,
          startOnBoot: true,
          enableHeadless: true,
          requiresBatteryNotLow: false,
          requiresCharging: false,
          requiresStorageNotLow: false,
          requiresDeviceIdle: false,
          requiredNetworkType: NetworkType.NONE,

        ),
        _onBackgroundFetch,
        _onBackgroundFetchTimeout);
    BackgroundFetch.scheduleTask(TaskConfig(
        taskId: "test",
        delay: 1000,
        periodic: false,
        stopOnTerminate: false,
        enableHeadless: true));
    BackgroundFetch.start();
  }

  void _onBackgroundFetchTimeout(String taskId) {
    //BackgroundFetch.finish(taskId);


    }
    
      void _onBackgroundFetch(String taskId) async {
        if (taskId == "test") {
        

  random();
      createNotification();
    }
  }

}

in the document said if we want app work even after terminate, we should set the "stopOnTerminate" equals false. but still not working. how can fix this? please help.

sadegh
  • 103
  • 5
  • It works after every 15mins. You can try to show a notification everytime a background task is successful. – Md. Kamrul Amin Jan 10 '23 at 10:07
  • I know. and when my app open or in background, the notifications show every 15 min. But when I close/terminate my app. no notifications show anymore. – sadegh Jan 10 '23 at 11:58
  • What device are you using to test this? – Md. Kamrul Amin Jan 11 '23 at 04:24
  • in android studio for emulator I use "pixel 3a API 29". and for real device I use my own phone (xiaomi mi 9t pro) – sadegh Jan 11 '23 at 05:06
  • 1
    Chinese devices such as Xiaomi, Redmi force stops the app when you terminate it from recent apps. You need to turn off force stopping of apps in one of your xiaomi apps. I am assuming you configured background service properly. – Md. Kamrul Amin Jan 11 '23 at 05:29
  • thank you. So is the anyway to manage this for every model of smart phone with programming? or every user should change own setting after install apps? – sadegh Jan 11 '23 at 07:15
  • 1
    Every user has to do it on their own. There is no other way around it. – Md. Kamrul Amin Jan 11 '23 at 08:52
  • In the example in pub.dev the BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask); is after runApp – Kobi Feb 08 '23 at 06:19
  • The task name is "flutter_background_fetch" and not "test"? – Kobi Feb 08 '23 at 07:53
  • I change the task-id to test. and I don't think the problem is related to the name. – sadegh Feb 19 '23 at 05:11

0 Answers0