transistorsoft / react-native-background-fetch

Periodic callbacks in the background for both IOS and Android
MIT License
1.43k stars 191 forks source link

It only seems to work while debugging. It won't in release #444

Closed abdullahIsa closed 1 year ago

abdullahIsa commented 1 year ago

Hello everything working well in dev mode but in production/release mode does not seem to work

Code

import React, {
  useContext,
  createContext,
  useCallback,
  useState,
  useEffect,
} from 'react';
import BackgroundFetch from 'react-native-background-fetch';
import {backgroundDataGetter} from './accountHelper';

const DailyTaskHookContext = createContext({});

export const DailyTaskContextProvider = ({children}) => {
  const [isAuthenticated, setIsAuthenticated] = useState(false);

  useEffect(() => {
    const taskCallback = async taskId => {
      console.log('Background fetch started');
      await backgroundDataGetter();
      BackgroundFetch.finish(taskId);
      console.log('Background fetch finished');
    };

    if (isAuthenticated) {
      BackgroundFetch.configure(
        {
          minimumFetchInterval: 15, // 15 minutes
          stopOnTerminate: false,
          enableHeadless: true,
          forceAlarmManager: true,
          requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY,
        },
        taskCallback,
        error => console.log('Background fetch failed to start', error),
      );
      BackgroundFetch.start();
      console.log('Background fetch initiated');
    } else {
      BackgroundFetch.stop();
      console.log('Background fetch stopped');
    }

    return () => {
      BackgroundFetch.stop();
    };
  }, [isAuthenticated]);

  const startDailyTask = useCallback(async () => {
    if (__DEV__ !== true) {
      BackgroundFetch.stop();
      setIsAuthenticated(true);
    }
  }, []);

  const stopDailyTask = useCallback(async () => {
    setIsAuthenticated(false);
  }, []);

  return (
    <DailyTaskHookContext.Provider
      value={{
        startDailyTask,
        stopDailyTask,
      }}>
      {children}
    </DailyTaskHookContext.Provider>
  );
};

export const useDailyTaskHook = () => useContext(DailyTaskHookContext);

i got no idea what is wrong as no error or anything.

christocracy commented 1 year ago

Ignored issue template. You’re not even saying if this is iOS or Android.