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

Task is not working on the background or the screen is locked #77

Open
rajeshkumar-kprinfo opened this issue Nov 28, 2023 · 4 comments
Open

Comments

@rajeshkumar-kprinfo
Copy link

rajeshkumar-kprinfo commented Nov 28, 2023

I have tried to get the location when the app is in background or the screen is locked/closed it works good on the development when i take the build and run the application the task that i have added is not executing. How to solve this issue i have given my code below.

  ReactNativeForegroundService.register({id: 1244});

  const addTask = () => {
    ReactNativeForegroundService.add_task(
      () => {
        console.log('task executed');
        getLocation();
      },
      {
        delay: 15000,
        onLoop: true,
        taskId: 1244,
      },
    );
  };

  const startForegroundService = () => {
    addTask();
    ReactNativeForegroundService.update({
      id: 1244,
      title: 'Sales 10X',
      message: 'Sales 10X is running in background for live tracking ',
      icon: 'ic_launcher',
    });
  };

  const stopForegroundService = () => {
    ReactNativeForegroundService.stop(1244);
  };

  const appState = useRef(AppState.currentState);
  const appStateSubscription = useRef(null);

  const handleAppStateChange = nextAppState => {
    if (
      appState.current.match(/inactive|background/) &&
      nextAppState === 'active'
    ) {
      console.log(
        'App has come to the foreground! Stopping foreground service.',
      );
      stopForegroundService();
    } else if (nextAppState === 'background') {
      console.log('App is in the background! Starting foreground service.');
      startForegroundService();
    }

    appState.current = nextAppState;
    console.log('AppState', appState.current);
  };

  useEffect(() => {
    let data = userDetailData?.userDetails;
    if (
      data &&
      data?.permission_list &&
      data?.permission_list?.includes('usertracking')
    ) {
      console.log('change in app state');
      appStateSubscription.current = AppState.addEventListener(
        'change',
        handleAppStateChange,
      );
    }
    return () => {
      appStateSubscription.current?.remove();
      ReactNativeForegroundService.stop(1244);
      // RNLocation.stopUpdatingLocation();
    };
  }, [userDetailData]);
@Saliheen
Copy link

I am also facing same issue

@anjan-daffo88
Copy link

+1

@rajeshkumar-kprinfo
Copy link
Author

Is anyone facing the same issue ?
If so is there any solution to solve this issue ?

@nilaybrahmbhatt
Copy link

I am looking for solution for the same. any one has?

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

4 participants