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.