Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BackgroundTimer stops when app goes to background after Few Sec in IOS React Native IOS #397

Open
Omrbhatti opened this issue Feb 9, 2022 · 18 comments

Comments

@Omrbhatti
Copy link

No description provided.

@DCaiena
Copy link

DCaiena commented Feb 14, 2022

same issue in any method

@cw0516
Copy link

cw0516 commented Mar 6, 2022

Same issue in android too😭

@alicja-mruk
Copy link

any updates?

@bwoodlt
Copy link

bwoodlt commented Apr 22, 2022

Any updates on this issue?

@marsieCohen
Copy link

This is still an issue, any updates?

@kaushikipandey
Copy link

yes...same issue! any solution?

@Simoon-F
Copy link

Simoon-F commented Aug 5, 2022

same, ios

@TranLuongTuanAnh
Copy link

any solution?

@mirsahib
Copy link

same here using
rn-0.67
tested on xaomi redmi note 7

@rgcjhn
Copy link

rgcjhn commented Aug 29, 2022

You dont need this library. You can achieve the same thing with Date, AppState and the built in setInterval.

@jamesdmurphy51
Copy link

Same issue for me - is there an alternative library anyone can recommend?

@fukemy
Copy link

fukemy commented Sep 23, 2022

no solution, IOS KILL ALL BACKGROUND METHOD after 30 seconds

@vmia159
Copy link

vmia159 commented Nov 30, 2022

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.

@Aaronphy
Copy link

Aaronphy commented Jan 9, 2023

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?

@rgcjhn
Copy link

rgcjhn commented Feb 14, 2023

@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 <></>;
};

@vmia159
Copy link

vmia159 commented Apr 6, 2023

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.

  1. When you start the timer, record the system uptime & boot time (current - uptime) when start.
  2. Use JS interval to get native system uptime.
  3. Save the current elapsed time by calculate the difference between current uptime and start uptime.
  4. 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;
}

@Qurat-ul-ainn
Copy link

Same issue
any solution Please?

@omerabdelhadyy
Copy link

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())"
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests