I found a library which works well in Android 12 even while the screen is off:
https://github.com/ocetnik/react-native-background-timer
Follow the installation and call the function like so:
import BackgroundTimer from "react-native-background-timer";
useEffect(() => {
const timeoutId = BackgroundTimer.runBackgroundTimer(async () => {
callYourFunction(); // Call your Function Here
}, 10000);
// 10000 is 10 Seconds
return () => BackgroundTimer.clearInterval(timeoutId);
}, []);
This also works in iOS but stops if you keep the app in the background for longer than two minutes. I have no idea why.
(Also I tried https://github.com/Rapsssito/react-native-background-actions library, but this also has same problem in iOS, and it doesn't work in Android).