ionic-team / ionic-team/capacitor-background-runner
Background runner not running when app is in background
- Dominant language
- C
- Stars
- 56
- Forks
- 36
- Avg merge
- 4d 12h
- Merged PRs (30d)
- 7
Description
Hi,
i am trying to make this work for 3 days now, i am sure that is something small but i can't figure it out.
So here we go:
I am using it on iOS, i made my configurations as in the documentation
- capacitor.config.ts
```js
plugins: {
BackgroundRunner: {
label: 'ro.patrimonium.opla.task',
src: 'runners/runner.js',
event: 'checkIn',
repeat: true,
interval: 1,
autoStart: true,
},
...
}
```
- AppDelegate.swift i don't need the notificatons part
```js
import UIKit
import Capacitor
import CapacitorBackgroundRunner
import WonderPush
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
BackgroundRunnerPlugin.registerBackgroundTask()
BackgroundRunnerPlugin.handleApplicationDidFinishLaunching(launchOptions: launchOptions)
return true
}
```
- runner.js
```js
addEventListener('checkIn', async (resolve, reject, args) => {
try {
if (args && typeof args.identifier !== 'undefined') {
await CapacitorKV.set('radius', args.radius)
await CapacitorKV.set('language', args.language)
await CapacitorKV.set('identifier', args.identifier)
}
const push = await CapacitorKV.get('identifier').value;
const radius = await CapacitorKV.get('radius').value
const language = await CapacitorKV.get('language').value
const location = await CapacitorGeolocation.getCurrentPosition();
// const location = {
// latitude: 45.565656,
// longitude: 45.5656565,
// };
console.log('this is my location ' + JSON.stringify(location))
const lat = location.latitude
const lng = location.longitude
const body = `identifier=${push}&radius=${radius}&language=${language}&lat=${lat}&lng=${lng}`;
console.log("location lat: " + lat);
console.log("location lng: " + lng);
console.log("language: " + language);
console.log("radius: " + radius);
const response = await fetch('https://my.endpoint', {
method: 'POST',
headers: {
Accept: 'application/json',
Authorization: `Bearer token`,
},
body: body
})
const message = await response.json()
console.log(JSON.stringify(message))
if (response.ok) {
console.log('Fetch is working!')
} else {
console.log('There is an error with the fetch function')
}
resolve(message)
} catch (err) {
console.error(err);
reject(err);
}
});
```
- Info.plist
```xml
BGTaskSchedulerPermittedIdentifiers
ro.patrimonium.opla.task
NSLocationAlwaysAndWhenInUseUsageDescription
We need to track your location while your device is locked in order to notify you when you are close to a monument.
NSLocationAlwaysUsageDescription
$(PRODUCT_NAME) uses location services to track your location in order to notify you when you are close to a monument
NSLocationWhenInUseUsageDescription
$(PRODUCT_NAME) uses location services to track your location in order to notify you when you are close to a monument
UIBackgroundModes
fetch
location
processing
remote-notification
```
And now comes the interesting part, if i dispatch the event from the app when it's state is active all is working ok, i am getting the location and it is sent to the dashboard, but when a send the app in background i have 2 strange problems:
1. I am using xcode to build the app and send it to my device (not simulator) and i am monitoring the logs in xcode, the background runner is getting called but i am getting an error for the location, CapacitorGeolocation.getCurrentPosition() cant get my location, all the permissions are allowed, but when the permission promt comes up i cant choose location always only while using the app, Allow once ...
If i edit my settings on the phone to have the app location always then it is working.
**So my question is what should i do so the user has the option to allow always the location**
2. If i stop the app execution from xcode and start the app from my device without monitoring it in xcode the background runner event is not triggered at all, i can see the location icon on my phone that pops up for some seconds but no data is sent to dashboard, when i activate the app then all calls are getting triggered and i am getting 4-5 requests to the dashboard.
I has thinking that CapacitorGeolocation.getCurrentPosition() can't get the location in the 30 seconds so i commented it and added manual location data, but even so the fetch is not getting triggered.
Did you have this problem ? Could somebody help me out please. I am using Ionic 7 with vuejs and capacitor 5
Thank you.
Contributor guide
Assessment
This issue has not been assessed yet.