Description
Currently TimingModule related operations don't work reliably in background tasks, as noted in the docs
The function passed to setTimeout does not always behave as expected. Instead the function is called only when the application is launched again. If you only need to wait, use the retry functionality.
this issue invertase/react-native-firebase#3955
Throwing a headlessJsTaskError seems the only way to make setTimeout work in the headless js task.
and this StackOverflow question.
I am developing a react-native applicaton with a headless js task to make some background work.
I need to execute some code after an amount of time, but noticed that using setTimeout execution gets suspended until the app is again in foreground.
Anyone has idea why this happens and know how to solve or workaround this?
I myself recently stumbled upon this issue as well and went into debugging it. For my findings and the solution I'll create a PR shortly.
Version
all
Output of npx react-native info
Issue is independent of build system and react-native version.
Steps to reproduce
Create a headless JS task using some TimingModule related code (for example setTimeout) and execute it while the app is in background (for example using a FCM data-only message).
Snack, code example, screenshot, or link to a repository
Example headless JS task handler:
module.exports = async (taskData) => {
console.log('started headless JS task')
await new Promise(resolve => setTimeout(resolve, 1))
console.log('resolved timeout')
}
Description
Currently
TimingModulerelated operations don't work reliably in background tasks, as noted in the docsthis issue invertase/react-native-firebase#3955
and this StackOverflow question.
I myself recently stumbled upon this issue as well and went into debugging it. For my findings and the solution I'll create a PR shortly.
Version
all
Output of
npx react-native infoIssue is independent of build system and
react-nativeversion.Steps to reproduce
Create a headless JS task using some
TimingModulerelated code (for examplesetTimeout) and execute it while the app is in background (for example using a FCM data-only message).Snack, code example, screenshot, or link to a repository
Example headless JS task handler: