Raja0sama / rn-foreground-service

Foreground Service for React Native made with ❤
https://rn-foreground.vercel.app/
147 stars 70 forks source link

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

Open rajeshkumar-kprinfo opened 11 months ago

rajeshkumar-kprinfo commented 11 months ago

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 commented 11 months ago

I am also facing same issue

anjan-daffo88 commented 10 months ago

+1

rajeshkumar-kprinfo commented 9 months ago

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

nilaybrahmbhatt commented 8 months ago

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