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 doesn't work when the app is closed or terminated. #469

Closed pankaj-dharmik closed 7 months ago

pankaj-dharmik commented 7 months ago

Can you please help me. I want to display notification after every 15 mins even when the app is terminated or closed. It works fine when the app is open but it doesn't work when the app is closed or terminated.

Your Environment

Expected Behavior

It should work even when the app is closed or terminated.

Actual Behavior

It work only when the app is running is the foreground.

Context

I want to make an app that gives notification after every 15 mins in loop even when the app is closed or terminated.

My Code: (onDisplayNotification is used to just display notification). ` const addEvent = (taskId) => { const timestamp = new Date().toLocaleString(); setEvents((prevEvents) => [...prevEvents, { taskId, timestamp }]); };

const initBackgroundFetch = async () => { const onEvent = async (taskId) => { console.log('[BackgroundFetch] Task:', taskId); onDisplayNotification() await addEvent(taskId); BackgroundFetch.finish(taskId); };

const onTimeout = async (taskId) => {
  console.warn('[BackgroundFetch] TIMEOUT Task:', taskId);
  BackgroundFetch.finish(taskId);
};

const status = await BackgroundFetch.configure(
  {
    minimumFetchInterval: 15,
    startOnBoot: true,
    stopOnTerminate: false,
    enableHeadless: true
  },
  onEvent,
  onTimeout,
);

console.log('[BackgroundFetch] Configure Status:', status);

};

useEffect(() => { initBackgroundFetch(); onDisplayNotification() }, []); `