Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background runner not running when app is in background #65

Open
kodyxgen opened this issue Nov 16, 2023 · 2 comments
Open

Background runner not running when app is in background #65

kodyxgen opened this issue Nov 16, 2023 · 2 comments

Comments

@kodyxgen
Copy link

kodyxgen commented Nov 16, 2023

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.

@ManiMohan-Radhakrishnan

hi.

@diegolaciar
Copy link

+1 , same issue here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants