react-native-background-timer
react-native-background-timer copied to clipboard
BackgroundTimer stops when app goes to background after Few Sec in IOS React Native IOS
same issue in any method
Same issue in android too😭
any updates?
Any updates on this issue?
This is still an issue, any updates?
yes...same issue! any solution?
same, ios
any solution?
same here using rn-0.67 tested on xaomi redmi note 7
You dont need this library. You can achieve the same thing with Date, AppState and the built in setInterval.
Same issue for me - is there an alternative library anyone can recommend?
no solution, IOS KILL ALL BACKGROUND METHOD after 30 seconds
Although this defeat the purpose of using this library The closest thing is restart and calculate the timer by comparing the difference of a locally stored start time and current time when it the app comes back to the foreground.
You dont need this library. You can achieve the same thing with Date, AppState and the built in setInterval.
Could you provide some showcase code?
@Aaronphy check this and maybe customized it because this was an old code
const HandlerComponent = () => {
const lastKnownTime = useRef();
const subscription = useRef();
const appState = useRef(AppState.currentState);
useEffect(() => {
lastKnownTime.current = moment().format('x');
subscription.current = AppState.addEventListener('change', nextAppState => {
const isForeground =
appState.current.match(/inactive|background/) &&
nextAppState === 'active';
if (isForeground) {
const duration = getDuration(lastKnownTime.current); // <- duration while in background in seconds
} else {
lastKnownTime.current = moment().format('x');
}
});
return () => subscription.current?.remove();
}, []);
const getDuration = unix => {
return Math.floor(
moment.duration(moment().diff(moment(unix, 'x'))).asSeconds(),
);
};
return <></>;
};
This is for timer purpose only and cannot do stuff
If you want to prevent someone that want trick your system, you can create a package to bridge native system uptime and use that instead of js moment/Date.
- When you start the timer, record the system uptime & boot time (current - uptime) when start.
- Use JS interval to get native system uptime.
- Save the current elapsed time by calculate the difference between current uptime and start uptime.
- In case someone change the clock manually. When the boot time is not the same, just pause the timer and show the last saved result.
For android
SystemClock.elapsedRealtime();
For IOS
#include <sys/sysctl.h>
- (time_t)uptime
{
struct timeval boottime;
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
size_t size = sizeof(boottime);
time_t now;
time_t uptime = -1;
(void)time(&now);
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) {
uptime = now - boottime.tv_sec;
}
return uptime;
}
Same issue any solution Please?
i am resolve this case to write the native swift code and call this code in react native code , and this work fine me app still working in background
@objc func startTimer(_ value: NSNumber) { print("Starting Timer with name: (value)")
// Capture self weakly to avoid retain cycles
timer = Timer.publish(every: TimeInterval(truncating: value), on: .main, in: .common)
.autoconnect()
.sink { [weak self] _ in
guard let self = self else { return }
print("Timer fired!")
let onIncrement = "onIncrement"
self.sendEvent(withName: onIncrement, body: ["message": "Timer fired with name \(value)!"])
self.timerMessage = "Timer fired at \(Date())"
}
}