ionic-team / capacitor-background-runner

Other
39 stars 24 forks source link

Background runner not running when app is in background #65

Open kodyxgen opened 11 months ago

kodyxgen commented 11 months ago

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

@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);
    }
});

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 commented 3 weeks ago

hi.

diegolaciar commented 2 weeks ago

+1 , same issue here.

CamKelly commented 1 week ago

I’m experiencing the same issue with my app. To verify if I was doing something wrong, I cloned and built the background-runner-testapp from the Ionic Team (found here: https://github.com/ionic-team/background-runner-testapp) using Xcode 16.0 and tested it on my iPhone running iOS 18.0. I can manually trigger my listener methods using dispatchEvent(), and they work as expected. However, they do not run automatically in the background, both in my app and in the background-runner-testapp.