Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Background runner not running when app is in background

Open
#65 6 comments 4 reactions 0 assignees View on GitHub

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:

  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.

Dominant language
C
Stars
56
Forks
36
Avg merge
3d 17h
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ionic-team/capacitor-background-runner

All issues in ionic-team/capacitor-background-runner

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.