Seeing errors with a basic attempt at using the plugin
In my background.js file i have 1 event listener defined:
addEventListener("myEvent", (details) => {
console.log("do something to update the system here:", details);
details.completed();
});
In my capacitor.config.ts i have Background Runner configured as follows:
BackgroundRunner: {
label: "com.myappname.background.task",
src: "background.js",
event: "myEvent",
repeat: false,
interval: 2,
autoStart: false,
}
In my React App i am using the @capacitor/app plugin to monitor appStateChange and am triggering myEvent based on the isActive status as follows:
App.addListener('appStateChange', async ({ isActive }) => {
if(!isActive) {
await BackgroundRunner.dispatchEvent({
label: "com.myappname.background.task",
event: "myEvent",
details: {foo: 'bar'},
});
}
});
Results in the following errors when running the app on an actual iOS device running iOS v16.5.1(c) after putting the app into the background:
starting runner and loading contexts...
2023-07-29 18:39:07.567069-0400 App[39138:2062252] *** Assertion failure in -[CLLocationManager setAllowsBackgroundLocationUpdates:], CLLocationManager.m:1033
2023-07-29 18:39:07.567933-0400 App[39138:2062252] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: !stayUp || CLClientIsBackgroundable(internal->fClient) || _CFMZEnabled()'
*** First throw call stack:
(0x1c0b98cb4 0x1b9c343d0 0x1bb32a56c 0x1cc5b1f28 0x10135d0d8 0x10135ce1c 0x101359274 0x10134e8a0 0x10134b400 0x10134c814 0x101352820 0x10134a600 0x101b7c520 0x101b7e038 0x101b92670 0x101b92e34 0x220918da0 0x220918b7c)
libc++abi: terminating due to uncaught exception of type NSException
What am i missing?
App.addListener isn't needed here, I explained more in detail here:
https://github.com/ionic-team/capacitor-background-runner/issues/20
There are some techniques and tools to better debug / manually trigger background tasks on iOS and Android, I'll work on adding those steps to the documentation.
Regarding the error, it could be crashing due to the fact that you have not requested permissions for accessing the Geolocation APIs, though your runner function isn't using geolocation. I'll look closer into this and work on getting a fix out.
I was able to get the exact implementation i noted above to work without any errors by enabling "Location Updates" in "Signing & Capabilities" in xCode, but obviously this is not ideal for anyone that has no need for accessing Geo Location data from a user's device.
To fix the "Invalid parameter not satisfying: !stayUp" error I also had to add the label of the task to the iOS info.plist like this:
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.capacitor.background.check</string>
</array>
Any news on this issue? My app crashes if I don't enable Location Updates. If I do enable the capability the plugin will ask for Location permission without the possibility to prevent it. I don't need to use location in my application :)
I fixed it temporarily by patching the dependency and commenting out this line
Where to place the file background.js in ionic react app?
@theproducer I got the same error and i got it working after enabling geolocation. When will it be resolved to work without geolocation?
@Coder-gunjan, place the file inside the public folder instead of src folder for react applications.
Resolved the error mentioned by following the solution mentioned in this thread https://github.com/mauron85/react-native-background-geolocation/issues/60
But ideally, it should not throw errors related to location if we are not using it.