Background runner not running when app is in background
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 30/100
- Issue type
- Bug
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- ios, javascript, swift, typescript
- Domain
- mobile
Research direction
Start by reviewing capacitor.config.ts, AppDelegate.swift, runner.js, and Info.plist, then reproduce the two reported paths: running under Xcode and launching directly on the device. Compare background-runner, location, and fetch behavior with the provided configuration; done means the event consistently runs outside Xcode and the reported location and request behavior is explained or corrected.
Written by the indexing model from the issue text.
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
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
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
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
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>ro.patrimonium.opla.task</string>
</array>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>We need to track your location while your device is locked in order to notify you when you are close to a monument.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) uses location services to track your location in order to notify you when you are close to a monument</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) uses location services to track your location in order to notify you when you are close to a monument</string>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
<string>processing</string>
<string>remote-notification</string>
</array>
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:
- 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 - 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.
- Dominant language
- C
- Stars
- 56
- Forks
- 36
- Avg merge
- 3d 17h
- Merged PRs (30d)
- 3
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ionic-team/capacitor-background-runner
-
[Bug]: android notification channel id not used in CapacitorNotifications.schedulePossibly taken @coffeemakr claimed this 20 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
ionic-team/capacitor-background-runner#158 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
ionic-team/capacitor-background-runner#155 · 1 reaction ·
-
Capacitor keeps running old background runnersMay be free again @theproducer claimed this 263 days ago, and no pull request is open. Openbug
ionic-team/capacitor-background-runner#152 · 1 comment · 2 reactions · 1 assignee ·
All issues in ionic-team/capacitor-background-runner
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
micropython/micropython#19786 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
vercel-labs/zerolang#442 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
triage
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
Maintainers usually reply within 1 day
-
mewencode: base256[] and base128[] are indexed out of bounds by a non-ASCII octetPossibly taken @kazu-yamamoto claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
kazu-yamamoto/Mew#276 ·
Maintainers usually reply within 1 day