Coder Social home page Coder Social logo

Comments (8)

theproducer avatar theproducer commented on May 10, 2024

Hello! Have you tried executing your runner from the foreground by calling dispatchEvent("myCustomEvent")?
You can see an example here:

const response = await BackgroundRunner.dispatchEvent({

If there is any problem, it should throw an error.

from capacitor-background-runner.

patrick-tresp avatar patrick-tresp commented on May 10, 2024

Hey, thanks for the response,

I implemented a dispatchEvent("myCustomEvent") in code and don't get an error, but interesting response:

{"__zone_symbol__state":null,"__zone_symbol__value":[]}

and not expected log from listener

console.warn('do something to update the system here');

i also copied exactly your examples from background.js which lead to same result…

from capacitor-background-runner.

theproducer avatar theproducer commented on May 10, 2024

Sorry, and by "response", I mean did you see the console statement appear in Xcode's output console?

from capacitor-background-runner.

patrick-tresp avatar patrick-tresp commented on May 10, 2024

Yes :)

const response= BackgroundRunner.dispatchEvent({ /****/ })
console.log("response", response )

i will keep trying…

from capacitor-background-runner.

theproducer avatar theproducer commented on May 10, 2024

No, if you are calling dispatchEvent('myCustomEvent'), and the handler for myCustomEvent is:

addEventListener('myCustomEvent', (resolve, reject, args) => {
    console.warn('do something to update the system here');
    resolve();
});

There should be no response except undefined really, and you should see the message 'do something to update the system here' in the Xcode console.

Do you see that message in the Xcode console?

from capacitor-background-runner.

patrick-tresp avatar patrick-tresp commented on May 10, 2024

I can see this message, when I dispatch the Event manually yes. It should be triggered by going into background mode when task is registered right?
But maybe we can narrow it down by finding out where the file "background-task.js" needs to be after compiling and cap-sync in App Folder.

working directory:
capacitor/background-task.js

dist folder:
dist/capacitor/background-task.js

after cap-sync:
ios/App/App/public/capacitor/background-task.js

i assume there is something misconfigured, that the file can not be found in runtime.

I also checked again Capacitor Object in scope and it does have BackgroundRunner in Plugins section

"Plugins": {
    "WebView": {},
    "Console": {},
    "CapacitorHttp": {},
    "CapacitorCookies": {},
    "PushNotifications": {},
    "BackgroundRunner": {}
  },

and all Methods are registered

{
      "name": "BackgroundRunner",
      "methods": [
        {
          "name": "addListener"
        },
        {
          "name": "removeListener"
        },
        {
          "name": "removeAllListeners",
          "rtype": "promise"
        },
        {
          "name": "checkPermissions",
          "rtype": "promise"
        },
        {
          "name": "requestPermissions",
          "rtype": "promise"
        },
        {
          "name": "dispatchEvent",
          "rtype": "promise"
        },
        {
          "name": "registerBackgroundTask",
          "rtype": "promise"
        },
        {
          "name": "checkPermissions",
          "rtype": "promise"
        },
        {
          "name": "requestPermissions",
          "rtype": "promise"
        }
      ]
    }

Can you share with me where your background-task.js is stored after cap-sync ?

from capacitor-background-runner.

patrick-tresp avatar patrick-tresp commented on May 10, 2024

After checking implementation again I guess it was meant to be used like this?

Or am I thinking wrong here? :)


if( runningInCapacitor ){
    App.addListener('appStateChange', (state) => {
        if (state.isActive) {
            console.log('App is active (foreground)');
        } else {
            console.log('App is in background');

            const response = BackgroundRunner.dispatchEvent({
                label: "de.domain.app.background.task",
                event: "myCustomEvent",
                details: {},
            });

            console.log("response ", response)
        }
    });
}

from capacitor-background-runner.

theproducer avatar theproducer commented on May 10, 2024

If you see the message in the console, then its able to find your JS file (otherwise it would print an error).

After checking implementation again I guess it was meant to be used like this?

No, part of what Background Runner does is set up the native code configuration and registration settings for you. You do not need to do anything with App.addListener - read this comment here: #20 (comment)

In short, the plugin when configured properly will call your specified event handler based on the settings in the plugin config, during times when your app is the background.

For example, your config:

BackgroundRunner: {
          label: 'de.domain.app.background.task',
          src: 'capacitor/background-task.js',
          event: 'myCustomEvent',
          repeat: true,
          interval: 5,
          autoStart: true, \\\ <-- this should be true if you want it to register your background task automatically.
      },

will automatically register your task with iOS and Android, which will execute your myCustomEvent event handler approximately every 5 minutes (15 minutes on Android - be sure you read this) while your app is in the background.

Im currently working on some more practical examples that show real world examples of this plugin in use, in the meanwhile feel free to ask for more help in our discord: https://discord.com/invite/UPYYRhtyzp

from capacitor-background-runner.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.