Background Fetch Event Received but task not executed when app is in background
Your Environment
-
Plugin version: 5.2.5
-
Platform: Android
-
Capacitor info (
npx cap doctor) Latest Dependencies:@capacitor/cli: 6.1.2 @capacitor/core: 6.1.2 @capacitor/android: 6.1.2 @capacitor/ios: 6.1.2
Installed Dependencies:
@capacitor/ios: not installed @capacitor/core: 5.7.4 @capacitor/android: 5.7.4 @capacitor/cli: 5.7.4
[success] Android looking great! 👌
-
Plugin config provided to
BackgroundFetch.configure: minimumFetchInterval: 15, // the fetch interval in minutes enableHeadless: true, // enable headless mode stopOnTerminate: false, // continue running even after the app terminates startOnBoot: true, // start on boot up forceAlarmManager: false, requiredNetworkType: BackgroundFetch.NETWORK_TYPE_NONE, requiresBatteryNotLow: false, requiresCharging: false, requiresDeviceIdle: false, requiresStorageNotLow: false
Expected Behavior
The app should be able to receive a background event and execute the associated background task in all app states: whether the app is active (in the foreground), running in the background (listed among recent apps), or even when the app has been terminated.
Actual Behavior
Currently, the background task is executed only in two cases: when the app is completely terminated (thanks to enableHeadless), where it correctly runs the code within BackgroundFetchHeadlessTask.java, and when the app is actively running in the foreground, performing the code in performYourWorkHere . However, when the app is closed but not fully terminated (i.e., still running in the background), the background event is received, but the task within performYourWorkHere only executes when the app is manually reopened. It seems as though the event is received, but the task waits for manual intervention to start. The desired behavior is for the task to execute every 15 minutes, even in this background state, without any manual trigger.
Debug logs
2024-09-11 15:52:24.325 12858-12858 TSBackgroundFetch D - Background Fetch event received: capacitor-background-fetch 2024-09-11 15:52:24.329 12858-12858 Capacitor/...etchPlugin V Notifying listeners for event fetch
2024-09-11 15:53:24.347 12858-12858 TSBackgroundFetch D [BGTask] timeout: capacitor-background-fetch 2024-09-11 15:53:24.351 12858-12858 Capacitor/...etchPlugin V Notifying listeners for event fetch
2024-09-11 16:01:52.346 12858-12858 Capacitor D App restarted 2024-09-11 16:01:52.435 12858-12858 Capacitor D App started 2024-09-11 16:01:52.451 12858-12858 TSBackgroundFetch D ☯️ onStart 2024-09-11 16:01:52.504 12858-12858 Capacitor/AppPlugin D Firing change: true 2024-09-11 16:01:52.506 12858-12858 Capacitor/AppPlugin V Notifying listeners for event appStateChange 2024-09-11 16:01:52.559 12858-12858 Capacitor/AppPlugin V Notifying listeners for event resume 2024-09-11 16:01:52.559 12858-12858 Capacitor/AppPlugin D No listeners found for event resume 2024-09-11 16:01:52.568 12858-12858 Capacitor D App resumed 2024-09-11 16:01:52.571 12858-12858 TSBackgroundFetch D ☯️ onResume 2024-09-11 16:01:53.103 12858-12888 OpenGLRenderer I Davey! duration=747ms; Flags=1, FrameTimelineVsyncId=58367, IntendedVsync=10942763596914, Vsync=10942996930238, InputEventId=0, HandleInputStart=10943005903508, AnimationStart=10943005923800, PerformTraversalsStart=10943005929466, DrawStart=10943245125341, FrameDeadline=10942780263580, FrameInterval=10943003677675, FrameStartTime=16666666, SyncQueued=10943304147800, SyncStart=10943308761425, IssueDrawCommandsStart=10943309830175, SwapBuffers=10943485484591, FrameCompleted=10943515494800, DequeueBufferDuration=3833, QueueBufferDuration=2074833, GpuCompleted=10943514337550, SwapBuffersCompleted=10943515494800, DisplayPresentTime=0, CommandSubmissionCompleted=10943485484591,
2024-09-11 16:01:53.153 12858-12858 Choreographer I Skipped 32 frames! The application may be doing too much work on its main thread. 2024-09-11 16:01:53.427 12858-12858 Capacitor/Console I File: https://localhost/assets/index-dBsS17A_.js - Line 6981 - Msg: [BackgroundFetch] EVENT: capacitor-background-fetch
Sounds like a case for useLegacyBridge: true
{
"appId": "xxx",
"appName": "xxx",
"webDir": "dist",
"server": {
"androidScheme": "https"
},
"plugins": {
"CapacitorHttp": {
"enabled": true
}
},
"android": {
"useLegacyBridge": true
}
}
I updated my capacitor.config.json file and nothing has changed, may I need to change something else, maybe in the AndroidManifest.xml?
may I need to change something else, maybe in the AndroidManifest.xml?
This plugin doesn't require anything from AndroidManifest.
I have no idea. I suggest you test the /example app here in this repo.
Actually "useLegacyBridge": true works! Yesterday I might forgot something else. Do you think I can simply change something in the plugin so that the code in BackgroundFetchHeadlessTask.java is performed also when the app is open or in the recent ones (instead of the performyourworkhere function)?
This could be useful to avoid redoing the same code in Javascript for that situation or creating a custom plugin for that specific situation
Do you think I can simply change something in the plugin so that the code in BackgroundFetchHeadlessTask.java is performed also when the app is open or in the recent ones
No. “Headless” means terminated. It only runs when your app is terminated..
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.