iOs 13: impossible to execute any background task when app is not foreground due to system policies
Closed this issue · 4 comments
Context
Works fine on Android.
On iOs, I want my app to keep storing the position received from the plugin even when in background.
I use the startTask/endTask pattern on the location event.
this.backgroundGeolocation.on(BackgroundGeolocationEvents.location)
.subscribe((location: BackgroundGeolocationResponse) => {
this.backgroundGeolocation.startTask().then((taskKey) => {
console.log('captured', location);
// my other code ... this.db.saveLocation(location);
this.backgroundGeolocation.endTask(taskKey);
});
}, error => {
this.utils.handleError(error);
});
Expected Behavior
Too see the callback being called an executed even in background.
Actual Behavior
Instead the app stop working after 5-10 s in background on iOs, until brought again in foreground.
Possible Fix
Update startTask / endTask method on iOs implementation to latest background mode support (didn't investigated).
Steps to Reproduce
- Install library
- Create page/component with configuration init and provided code.
- Run on iOs simulator
- Open safari developer console or xCode logger to see results, or empirically
Context
It's not possible to get a task execute while app in background on iOs.
Debug logs
2020-03-30 21:57:07:930 MyAppName[94612:1863122] DistanceFilterLocationProvider acquiring stationary location, accuracy: 5
2020-03-30 21:57:07:930 MyAppName[94612:1863122] DistanceFilterLocationProvider found most accurate stationary before timeout
2020-03-30 21:57:07:938 MyAppName[94612:1863122] DistanceFilterLocationProvider startMonitoringStationaryRegion {37.331333,-122.030660,1}
2020-03-30 21:57:07:947 MyAppName[94612:1863122] BgGeo #onStationaryChanged
2020-03-30 21:57:07:967 MyAppName[94612:1863122] BgGeo #onLocationChanged Location: id=(null) time=2020-03-30 19:57:07 +0000 lat=37.33133328 lon=-122.0306598 accu=5 aaccu=-1 speed=3.14 bear=1.44 alt=0
**2020-03-30 21:57:11.385496+0200 MyAppName[94612:1863122] [ProcessSuspension] 0x6000010e9d10 - WKProcessAssertionBackgroundTaskManager: Ignored request to start a new background task because the application is already in the background**
from now on nothing more on the log until app re-open.
And then on bring app in foreground:
2020-03-30 21:59:12.799985+0200 MyAppName[94612:1863122] THREAD WARNING: ['Console'] took '42.891113' ms. Plugin should use a background thread.
....
2020-03-30 21:59:12.877053+0200 MyAppName[94612:1863122] CDVBackgroundGeolocation #onStationaryChanged
2020-03-30 21:59:12.926172+0200 MyAppName[94612:1863122] CDVBackgroundGeolocation #onLocationChanged
...
2020-03-30 21:59:13.010784+0200 MyAppName[94612:1863122] [ProcessSuspension] 0x6000010e9d10 - WKProcessAssertionBackgroundTaskManager: Ignored request to start a new background task because the application is already in the background
2020-03-30 21:59:13.120080+0200 MyAppName[94612:1863122] [ProcessSuspension] 0x6000010e9d10 - WKProcessAssertionBackgroundTaskManager: Ignored request to start a new background task because the application is already in the background
2020-03-30 21:59:13.251840+0200 MyAppName[94612:1863122] captured [object Object]
...
2020-03-30 21:59:13.273778+0200 MyAppName[94612:1863122] Task ended
2020-03-30 21:59:12:356 MyAppName[94612:1863122] DistanceFilterLocationProvider switchMode 0
2020-03-30 21:59:15.378732+0200 MyAppName[94612:1916539] DistanceFilterLocationProvider switchMode 0
2020-03-30 21:59:16.774861+0200 MyAppName[94612:1863122] [ProcessSuspension] 0x6000010e9d10 - WKProcessAssertionBackgroundTaskManager: Ignored request to start a new background task because the application is already in the background
2020-03-30 21:59:12:818 MyAppName[94612:1863122] DistanceFilterLocationProvider didUpdateLocations (operationMode: 0)
I also tried to enable following iOS background modes with following permission (Location updates was already checked), but nothing changed
On further investigations I'm seeing that actually the mechanism, work SOMETIMES, but with timings really different between foreground mode and didn't really tested in deep yet.
To simulate the GPS sensor, I'm using the Location Simulator Feature of the emulator on a freeway ride mode, my config contains a short range in distance filter mode (5m).
The evidence from the logs is that the method it's being called with a ratio of 1/10, so if usually I got a callback request once 10s while on foreground, now I got one once two minutes during app in background. Sometimes the running thread sprint for a couple of seconds showing an aggregations of positions one after the other, but never more than 2, in my setup.
At the end of any operation cycle I got the error in xCode logger:
[ProcessSuspension] 0x600002a128b0 - WKProcessAssertionBackgroundTaskManager: Ignored request to start a new background task because the application is already in the background
At least, this work sufficiently as now.
<config-file parent="UIBackgroundModes" target="*-Info.plist">
<array>
<string>fetch</string>
</array>
<array>
<string>location</string>
</array>
<array>
<string>processing</string>
</array>
</config-file>
<edit-config file="*-Info.plist" mode="merge" target="NSLocationWhenInUseUsageDescription">
<string>need location access to find things nearby</string>
</edit-config>
@jackie-d - Give a try to this plugin - �
- https://www.npmjs.com/package/cordova-plugin-bg-geolocation
- https://github.com/Anuj-Raghuvanshi/cordova-plugin-bg-geolocation
Thanks!
The reported behaviour (decresing in background frequences frequncy) is possibly a mandatory store compliance specific.