Comments (4)
Hello!
For both iOS and Android, in what directory exactly is the background.js file supposed to live for each platform respectively?
It should in your build or dist folder - we are currently working on a process to help with this (and to facilitate using TypeScript for your runner file), but for now you'll need to copy your runner file into the appropriate directory. See our example test app:
In capacitor.config.ts is it possible to define multiple events in event: 'myCustomEvent'?
Or is it that we can only define one event to execute automatically?
No, that's currently not possible at the moment.
Does interval: 2 mean 2 minutes? or 2 seconds?
This is in minutes. Keep in mind there are other constraints on the interval in which the OS will call your background task (Limitations of Background Tasks), so the interval you set here may not be hit exactly when running on a device.
When and where is it appropriate to execute an event that is defined in our runner?
Is it OK to do this inside the @capacitor/app plugin's appStateChange listener event?
You don't need to do anything to get your event called once everything is set up properly. So long as autoStart
is true
, the plugin will automatically register and schedule your background task with the OS.
For example, if you have the following config:
{
autoStart: true,
event: "runPeriodicUpdate",
interval: 60,
repeat: true
}
Once the app is sent to the background and no longer active, after 1 hour - every hour (or thereabouts, if you are on iOS), the OS will execute the task, and the plugin will call the runPeriodicUpdate
event that has been defined in your runner js file.
dispatchEvent
can be called directly while your app is in the foreground if you want to execute any runner code on a separate background thread outside of the UI / WebView.
Let us know if that helps. We're continuing to update the docs, and just from typing this out I noticed a few things I need to tweak and add to the current docs. It may help if I created some visuals to help to explain the overall process too.
from capacitor-background-runner.
Hello!
For both iOS and Android, in what directory exactly is the background.js file supposed to live for each platform respectively?
It should in your build or dist folder - we are currently working on a process to help with this (and to facilitate using TypeScript for your runner file), but for now you'll need to copy your runner file into the appropriate directory. See our example test app:
This is not ideal in my case since i am using Ionic 7 whereas it compiles using vite. For the life of me i cannot remember how i ultimately got this to work, but i have placed my background.js runner file in my public directory, and in capacitor.config.ts i just define the src as src: "background.js". I feel like i am forgetting something else? But it is working even though i would imagine we do not want our runner to exist in the public directory. Better documentation for details like this would really be helpful. I cannot remember nor do i understand how my capacitor.config is able to find it sitting there in the public directory. I also have not tested any of this on Android yet, so it might very well be that my setup could be an issue.
In capacitor.config.ts is it possible to define multiple events in event: 'myCustomEvent'?
Or is it that we can only define one event to execute automatically?No, that's currently not possible at the moment.
Does interval: 2 mean 2 minutes? or 2 seconds?
This is in minutes. Keep in mind there are other constraints on the interval in which the OS will call your background task (Limitations of Background Tasks), so the interval you set here may not be hit exactly when running on a device.
When and where is it appropriate to execute an event that is defined in our runner?
Is it OK to do this inside the @capacitor/app plugin's appStateChange listener event?You don't need to do anything to get your event called once everything is set up properly. So long as
autoStart
istrue
, the plugin will automatically register and schedule your background task with the OS.For example, if you have the following config:
{ autoStart: true, event: "runPeriodicUpdate", interval: 60, repeat: true }Once the app is sent to the background and no longer active, after 1 hour - every hour (or thereabouts, if you are on iOS), the OS will execute the task, and the plugin will call the
runPeriodicUpdate
event that has been defined in your runner js file.
dispatchEvent
can be called directly while your app is in the foreground if you want to execute any runner code on a separate background thread outside of the UI / WebView.Let us know if that helps. We're continuing to update the docs, and just from typing this out I noticed a few things I need to tweak and add to the current docs. It may help if I created some visuals to help to explain the overall process too.
What i would like to know is if i can call a custom iOS Plugin that i created for my app using this basic capacitor guide which was really helpful. So far i have managed to avoid having to create an entire plugin package in-order to do some basic work in swift using that guide. Id like to be able to call a method that exists in MyPlugin.swift from the background runner.
from capacitor-background-runner.
Not sure where to reach out for further support, so asking here given the topic in focus being documentation.
How do we pass data that is retrieved inside the background runner to our native app through a method that exists in a custom plugin, be it for iOS or Android?
from capacitor-background-runner.
For the life of me i cannot remember how i ultimately got this to work, but i have placed my background.js runner file in my public directory, and in capacitor.config.ts i just define the src as src: "background.js". I feel like i am forgetting something else? But it is working even though i would imagine we do not want our runner to exist in the public directory. Better documentation for details like this would really be helpful.
Yeah, this sounds about right. The runner js file needs to be its own file outside the rest of your app JS bundle, and the plugin search its search from within the public directory. We'll come up with a more ergonomic way of handling this.
Id like to be able to call a method that exists in MyPlugin.swift from the background runner.
This isn't supported yet. Depending on what you are trying to do, your runner code can store bits of data in UserDefaults / SharedPreferences, then you access it in native code using the same APIs. Adding custom JS bindings to a underlying native function is something we can expose in the near future, but it's a rather advanced use case.
For more conservational questions and other support, you can come to the Ionic Discord, I keep an eye on it periodically during the day.
https://discord.com/invite/UPYYRhtyzp
from capacitor-background-runner.
Related Issues (20)
- Badly documented -
- Where to place the .js file HOT 2
- Fetch in android not working HOT 11
- How to call a function or url from runner.js HOT 1
- Background runner plugin: backgroundrunner.dispatchEvent error HOT 3
- bad issue...
- Fetch problem on iOS 17 simulator
- Background runner not running when app is in background
- What is the correct call in the BackgroundRunner for Geolocation
- How to acess other capacitor plugins like sqlite in backgroundTasks.
- Cannot use fetch inside scheduled runner HOT 2
- Wrong event triggered
- Bug?T ask :app:checkDebugAarMetadata FAILED HOT 1
- Android build HOT 3
- Documentation for actionTypeId
- Unable to dispatchEvent on android HOT 1
- Scheduled Notifications not appearing on android
- Plugin stops appStateChange not triggered on android
- Unable to connect a device using bluetoothle plugin HOT 4
- Frequent android app crashes when calling dispatchEvent HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from capacitor-background-runner.