transistorsoft/react-native-background-fetch

It doesn't work when the app is closed or terminated.

pankaj-dharmik opened this issue · 0 comments

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

  • Plugin version:4.2.1
  • Platform: Android
  • OS version: Android 11
  • Device manufacturer / model: K20 Pro
  • React Native version (react-native -v): 0.72.6

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()
}, []);
`