Код: Выделить всё
const BACKGROUND_TASK_INDENTIFIER = "background-task";
TaskManager.defineTask(BACKGROUND_TASK_INDENTIFIER, async () => {
const status = await BackgroundTask.getStatusAsync()
console.log("Executing background task", status)
return setTimeout(() => console.log("Status of Background task ", status), 3000)
})
async function registerBgTask(){
return BackgroundTask.registerTaskAsync(BACKGROUND_TASK_INDENTIFIER)
}
async function unregisterBgTask(){
return BackgroundTask.unregisterTaskAsync(BACKGROUND_TASK_INDENTIFIER)
}
// inside component
useEffect(() => {
const subscription = AppState.addEventListener("change", async (state) => {
if (state === "background") {
console.log("registering task...")
await registerBgTask() // the code in my task here doesn't run
} else {
console.log("unregistering task...")
await unregisterBgTask()
}
})
return () => subscription.remove()
}, [])
Подробнее здесь: https://stackoverflow.com/questions/797 ... native-app
Мобильная версия