react-native-background-fetch: Headless Task not working in Android

Your Environment

  • Plugin version:
  • Platform: Android
  • OS version: Android 13
  • Device manufacturer / model: Realme Narzo 30
  • React Native version (react-native -v): 0.72.6
  • react-native-background-fetch version: ^4.2.1
  • Plugin config

Main Component :

const MainComponent = props => {

  const initBackgroundHandler = async () => {
    const status = await BackgroundFetch.configure(
      {
        taskId: 'newTask',
        minimumFetchInterval: 15, // <-- minutes (15 is minimum allowed)
        stopOnTerminate: false,
        enableHeadless: true,
        startOnBoot: true,
      },
      async taskId => {
        console.log('get Realm data ', data);
        console.log('on Event taskId: ', taskId);
        //displayPushNotification(); // will call in headless task to show notification
      },
      taskId => {
        console.log('[Fetch] TIMEOUT taskId:', taskId);
        BackgroundFetch.finish(taskId);
      },
    );
    console.log('Background fetch status', status);
  };

  useEffect(() => {
    initBackgroundHandler();
    initPushNotifications(); // initial config to show notification
  }, []);

  return (
    <SafeAreaView>
      <View style={{padding: 10}}>
        // Some Code
      </View>
    </SafeAreaView>
  );
};

App.js :

import {NavigationContainer} from '@react-navigation/native';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import React, {useEffect} from 'react';
import EditRecord from './src/EditRecord';
import MainComponent from './src/MainComponent';
import ViewRecord from './src/ViewRecord';
import {useRealmContext} from './databases/RealmContext';
const Stack = createNativeStackNavigator();
const App = () => {
  const {RealmProvider} = useRealmContext();

  return (
    <RealmProvider>
      <NavigationContainer>
        <Stack.Navigator>
          <Stack.Screen name="Home" component={MainComponent} />
          <Stack.Screen name="ViewRecord" component={ViewRecord} />
          <Stack.Screen name="EditRecord" component={EditRecord} />
        </Stack.Navigator>
      </NavigationContainer>
    </RealmProvider>
  );
};
export default App;

Index.js:

import {AppRegistry} from 'react-native';
import BackgroundFetch from 'react-native-background-fetch';
import App from './App';
import {name as appName} from './app.json';

import {
  displayPushNotification,
  getDataFromAsync,
  sendDataToServer,
} from './src/Utils';

const MyHeadlessTask = async taskData => {
  console.log('[BackgroundFetch HeadlessTask] start: ', taskData);

  // Do something in the background.
  // For example, fetch new data or sync with the server.
  const dbData = await getDataFromAsync();
  console.log('get Realm data HeadlessTask', dbData);
  const serverResponse = await sendDataToServer(dbData);
  console.log('on HeadlessTask serverResponse', serverResponse[0].data);
  console.log('on Event taskId: HeadlessTask', taskData);
  displayPushNotification();

  console.log('[BackgroundFetch HeadlessTask] finish: ', taskData);
  BackgroundFetch.finish(taskData);
};

BackgroundFetch.registerHeadlessTask(MyHeadlessTask);
AppRegistry.registerComponent(appName, () => App);

I have gone through the docs many times not able to identify the issue, configured everything as mentioned in docs. I have attached the logs below. Running the app in real device. Please let me know if any other info is required.

Expected Behavior

Headless task should be called when app is in background or terminated.

Actual Behavior

Headless task is not getting called when app is terminated or in background only callback function gets called in when is open.

Context

I am trying to run task when app is in background or terminated after every 15mins.

Debug logs

Screenshot 2023-10-28 at 10 50 40 AM

Terminated the app at 10:23 waited till 10:50 ,nothing happened.

About this issue

  • Original URL
  • State: open
  • Created 8 months ago
  • Reactions: 1
  • Comments: 16 (8 by maintainers)

Most upvoted comments

15 min is not a guarantee. Let it run for 24 hours.

Don’t post screenshots of logs. Post text.

Your config is fine. Now go simulate tasks as documented in the readme. It’s easy.