Closed matheusyuri7 closed 1 year ago
You need to understand in the headless task, also known as the Background task, you don't have a react instance, rather just javascript execution; therefore whatever state you try to update would be useless. What you might be able to use is localstorage of the application, Why? ask yourself is that what you need?
Let's say you have a task, And whenever it gets a position, you need to update the notification, like a timer.
You would need to directly update the notification itself by using .update
method and update the notification with the new data.
here is an example that shows a progress bar on the notification and see how I am maintaining a state
{
ReactNativeForegroundService.add_task(() => update(), {
delay: 1000,
onLoop: true,
taskId: 'taskid',
onError: e => console.log(`Error logging:`, e),
});
let counter = 0;
const update = () => {
if (counter >= 100) {
ReactNativeForegroundService.stop();
}
counter += 2;
ReactNativeForegroundService.update({
id: 1244,
title: 'Foreground Service',
message: 'you are online!',
importance: 'min',
icon: 'ic_launcher_foreground',
button: true,
buttonText: 'WOah',
visibility: 'secret',
setOnlyAlertOnce: true,
buttonOnPress: 'cray',
progress: {
max: 100,
curr: counter,
},
});
};
}
Thanks again for the explanation @Raja0sama ! I was able to do what I need by using Async Storage.
Hello,
I'm developing a fitness app and I need to update some hooks in background (time, speed, location, calories, etc), but it doesn't update when I'm running on background. How can I set hooks and run useEffects in background?