Using Expo React Native, is there any way to run a stopwatch / timer in the background? - react-native

Problem
I've created a simple stopwatch app using Expo for React Native. When I leave the app (click the home button on iPhone), and return to the app, the stopwatch starts back up from the time when I left. For example, if the stopwatch is at 3 seconds and I leave the app for 5 seconds, when I return it is still at 3 seconds rather than 8.
Question
Using Expo, is there any way to run the stopwatch in the background? If yes, how? If no, what is the best workaround?
Resources
I've found similar questions that are dated, but I'm looking for something recent. Some of the resources mentioned in the other answers are the following:
https://docs.expo.io/versions/v35.0.0/sdk/background-fetch/
https://github.com/ocetnik/react-native-background-timer

It sounds like your best bet is to note the start time and persist it, e.g. using Expo's AsyncStorage. Then you can just display the time elapsed since this stored start time.

Related

React Native - Execute code in background on specific battery percentage

I have a react native application where the user can choose any battery level from 15 to 100. When his battery reaches the chosen value, javascript code should be executed.
Is there a way to do this when the app is in the background? I am aware I can have long running tasks, which will be displayed as a notification to the user and I can have my code logic there.
Are there any alternatives that do not include long running tasks?
My solution for this problem to register a BroadcastReceiver in android, which will be executed every time when the battery percentage changes. It works in background and if the battery reaches a certain threshhold, a headless task can be executed which will run javascript code.

Running cleanup when a react native app is closed

In my react-native app's code:
I am monitoring AppState changes
When AppState changes to 'inactive' or 'background', I write about 2000 key/value pairs of data to AsyncStorage
When the app starts I read this data
When I test the app on android (didn't test on iOS yet):
If I minimize the app, the data is written as expected. I can see that by closing the app after minimizing it, and restarting it
However, if I click on the 'Recent' button in the navigation bar (which changes AppState to 'inactive') and immediately click on the 'X' button at the top right of the app, the data doesn't seem to be written, or at least not all of it (I am not checking all 2000 values, just few of them). If I try to write only few values, they are written
Question:
The explanation to the above behavior seems to be simple: the app has enough time to perform few operations before I click on 'close', but not to write 2000 values. Is there a way to perform the writing before the app is closed?
Actually it's not recommended to do any async function or anything that could take too long. Because you don't have the control of how much time it will take to write the 2000 values. The app can go to background and the SO can kill the proccess of your app. When the app is in background is the SO that controls it.

How can i perform task even if the app is closed in React Native

I want to perform some task if the device is back online. I have tried with Broadcast receiver but its working perfectly when the app is running and the app is moved to background. but its not working when the app is closed :(.
please find the below code which i have tried..
import {DeviceEventEmitter,Platform} from 'react-native';
const func=(map)=>{
console.log("connection changed")
}
DeviceEventEmitter.addListener('GReferrerBroadcastReceiver', func);
Can anyone propose a solution for how to perform some task if the app is closed. I found Job scheduler in native android but how can i connect to React Native???
a help will be really appreciable :) ......
This library implement's background tasks. It's implementation is in JS and pretty easy to use: https://github.com/Rapsssito/react-native-background-actions
React-native-background-action works pretty well refer documentation from officially website but if u are looking for user tracking after start click then it fail after 10-15 minutes on phone lock...

Execute code at specific time in react native

I want to execute some function of my react native app at certain times, no matter if the app is opened or in background.
For example, execute the function at monday 3:00pm and wednesday 5:00pm
Is it possible to achieve this? Sorry if the question is noob. The alternative is to send a request from server side, but I want to know if is possible from the app side.
Thanks
Currently there is totally no support for background tasks, only for Android bit it is still limited: https://facebook.github.io/react-native/blog/2016/10/25/0-36-headless-js-the-keyboard-api-and-more.html
There is also limitation in React Native, when the app is in the background the js bridge stops getting messages.

Issue is request stops after sometime in Android Sencha app

i have issue in sencha application in which i called window.setInterval function
to get user message after some specific time it is working in iPhone,Desktop(chrome)but On Android device request stops after sometime.And working properly on iPhone & Desktop.
So please help me out.
Thank You.
I've found that on some Android devices, methods such as setInterval can be very unreliable and can often crash the browser completely.
To work around, I suggest you clear the interval every X seconds (depending on the amount of time you are using for your interval) and then restart the interval again.