0

I'm getting the time one time. sometimes I didn't get that too. I need the background time. once if I started the fetching background button it should give the time in the console until I stop the fetch background. I used expo background fetch and task manager to fetch the background time . I'm facing a problem it fetching time continuously so please help me with this coding. documentation is in expo documentation.

import * as BackgroundFetch from 'expo-background-fetch';
import * as TaskManager from 'expo-task-manager';

   
const BACKGROUND_FETCH_TASK = 'background-fetch1';

  const now = Date.now();

  console.log(`Got background fetch call at**** date: ${new Date(now).toISOString()}`);

  // Be sure to return the successful result type!
  return BackgroundFetch.Result.NewData;
});

async function registerBackgroundFetchAsync() {
   const now = Date.now();

  console.log(`Registered**** date: ${new Date(now).toISOString()}`);

  console.log(" registered ");
  return BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
    minimumInterval: 60 * 15, // 30 sec
    stopOnTerminate: false, // android only,
    startOnBoot: true, // android only
  });
}

async function unregisterBackgroundFetchAsync() {
  console.log("un registered ");
  const now = Date.now();

  console.log(`Un registered fetch call at**** date: ${new Date(now).toISOString()}`);

  return BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK);
}


const TrackScreen = ({ navigation }) => {
  const { state, clearError, fetchContactsforTrack } = useContext(TrackContext);
 const [isRegistered, setIsRegistered] = useState(false);
  const [status, setStatus] =useState(BackgroundFetch.Status);
      
  // Clear error if any
  useEffect(() => {
     checkStatusAsync();  
   
    const unsubscribe = navigation.addListener('focus', () => {
      clearError();
      
    });
    return unsubscribe;
  }, [navigation, clearError]);

  /* Stop the backgroud tracking */
  const stopTracking=()=>{
      
  }
   /* Stop the backgroud tracking */
  const startTracking=(track)=>{
    var  trackdata= fetchContactsforTrack(track)
  
  }
  const checkStatusAsync = async () => {
    const status = await BackgroundFetch.getStatusAsync();
    const isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_FETCH_TASK);
    const now = Date.now();

  console.log(`Checking statuscall at**** date: ${new Date(now).toISOString()}`);

    console.log("-------------"+status);
    console.log("-------------"+isRegistered);
    console.log("-------------"+JSON.stringify(BackgroundFetch));
    setStatus(status);
    setIsRegistered(isRegistered);
  };

  const toggleFetchTask = async () => {
    if (isRegistered) {
      await unregisterBackgroundFetchAsync();
    } else {        
      await registerBackgroundFetchAsync();
      await BackgroundFetch.setMinimumIntervalAsync(.5);
    }

    checkStatusAsync();
  };

  
  return (
         
 <View >
      <View >
        <Text>
          Background fetch status:{' '}
          <Text >{status ? BackgroundFetch.Status[status] : null}</Text>
        </Text>
        <Text>
          Background fetch task name:{' '}
          <Text >
            {isRegistered ? BACKGROUND_FETCH_TASK : 'Not registered yet!'}
          </Text>
        </Text>
      </View>
      <View ></View>
      <Button
        title={isRegistered ? 'Unregister BackgroundFetch task' : 'Register BackgroundFetch task'}
        onPress={toggleFetchTask}
      />
    </View>

);

this is my code
sagi
  • 40,026
  • 6
  • 59
  • 84
Divya B
  • 9
  • 3
  • 1
    Your should tag the correct used language – sagi Oct 26 '21 at 10:25
  • I am facing the same problem myself. The background fetch function gets called only once and sometimes never. Hoping to get revelant answers after placing the bounty – TheFishTheySay Mar 14 '22 at 11:23
  • please read the docs [https://docs.expo.dev/versions/latest/sdk/background-fetch/#triggering-background-fetches](https://docs.expo.dev/versions/latest/sdk/background-fetch/#triggering-background-fetches) – fixedDrill Mar 14 '22 at 11:53
  • I did indeed follow that but it did not get fixed – TheFishTheySay Mar 14 '22 at 13:19

1 Answers1

2

Your function might not run immediately after a second or so. This is because the minimum interval of time is 15 minutes. Android automatically switches it to 15 minutes if the value of the interval is less than the minimum.

Maybe this would help - https://proandroiddev.com/android-restrictions-you-may-encounter-during-development-process-c39ede513813

BladeOfLightX
  • 504
  • 4
  • 17