transistorsoft / react-native-background-fetch

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

Tasks are not running in background - Android #465

Closed jcschmittmouts closed 2 weeks ago

jcschmittmouts commented 8 months ago
useEffect(() => {
        BackgroundFetch.configure(
            {
                enableHeadless: true,
                forceAlarmManager: true,
                minimumFetchInterval: TOTAL_TIMER,
                stopOnTerminate: true,
                startOnBoot: false,
            },
            async (taskId) => {
                await sendEvent(12);
                BackgroundFetch.finish(taskId);
            },
            async (taskId) => {
                BackgroundFetch.finish(taskId);
            }
        );

        BackgroundFetch.scheduleTask({
            forceAlarmManager: true,
            taskId: "sendEvents",
            delay: 1000,
            periodic: false,
        });

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

Expected Behavior

The expected behavior is to execute the task every 10 minutes (TOTAL_TIMER = 10) even if my app is not in foreground.

Actual Behavior

The task is not running in background, but if I put my app in foreground, all those tasks that was not ran are being called at the same time.

Steps to Reproduce

Configure the BackgroundFetch as shown in the Plugin config section above.

Context

Run a task every 10 minutes even if my app is not in foreground.

jcschmittmouts commented 8 months ago

Debugging it I realized that the tasks are being called correctly. But inside my task there's a http request to my backend server that is not exectuted for some reason and all those requests are being called at the same time when I put my app in foreground. Anybody who knows why this occurs? There's a way to avoid this behavior?

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. ` async function onDisplayNotification() { // Request permissions (required for iOS) await notifee.requestPermission() console.log("Notification run")

// Create a channel (required for Android)
const channelId = await notifee.createChannel({
  id: 'default',
  name: 'Default Channel',
});

// Display a notification
await notifee.displayNotification({
  title: 'Notification Title',
  body: 'Main body content of the notification',
  android: {
    channelId,
    // smallIcon: 'name-of-a-small-icon', // optional, defaults to 'ic_launcher'.
    // pressAction is needed if you want the notification to open the app when pressed
    pressAction: {
      id: 'default',
    },
  },
});

}

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

github-actions[bot] commented 4 weeks ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 2 weeks ago

This issue was closed because it has been inactive for 14 days since being marked as stale.