Comments (8)
Hello! Have you tried executing your runner from the foreground by calling dispatchEvent("myCustomEvent")
?
You can see an example here:
If there is any problem, it should throw an error.
from capacitor-background-runner.
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.
Sorry, and by "response", I mean did you see the console statement appear in Xcode's output console?
from capacitor-background-runner.
Yes :)
const response= BackgroundRunner.dispatchEvent({ /****/ })
console.log("response", response )
i will keep trying…
from capacitor-background-runner.
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.
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.
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.
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)
- Add missing properties to Response object returned by fetch api.
- Missing code / permissions on example-app and app crash on geolocation event HOT 2
- Confusing runner src file location HOT 1
- Events can be called but are not scheduled automatically HOT 3
- Modify the badge with the background runner HOT 2
- Some relevant details are missing in the documentation
- Android receives the notification but does not open the APP when tap at the notification
- Doubt about use HOT 1
- Error getting location on background HOT 1
- 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.
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.