react-native-background-actions issue for ios

Viewed 433

Iam using react-native-background-actions to run a background task. but when I exit app, the task just run in 30s and stop How I can resolve to run forever, atleast untill app terminated

In my code, after 1s, increate 'COUNT' and save to Storage

// BackgroundTaskService.js
  import AsyncStorage from '@react-native-community/async-storage';
  import BackgroundService from 'react-native-background-actions';
  import BackgroundJob from 'react-native-background-actions';

  let sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

  const increaseCountTask = async taskDataArguments => {
    const {delay} = taskDataArguments;
    await new Promise(async resolve => {
      for (let i = 0; BackgroundJob.isRunning(); i++) {

        var value = await AsyncStorage.getItem('COUNT');
        if(!value) {
          await AsyncStorage.setItem('COUNT', "2");
        }
        var value = await AsyncStorage.getItem('COUNT');
        await AsyncStorage.setItem('COUNT', (parseInt(value) + 1).toString());
        value = await AsyncStorage.getItem('COUNT');
        console.log('value', value);
        await sleep(delay);

      }
    });
  };
  const options = {
    taskName: 'Demo',
    taskTitle: 'Demo Running',
    taskDesc: 'Demo',
    taskIcon: {
      name: 'ic_launcher',
      type: 'mipmap',
    },
    color: '#ff00ff',
    parameters: {
      delay: 1000,
    },
    actions: '["Exit"]',
  };
  const start = () => {
    BackgroundService.start(increaseCountTask, options);
  };
  const stop = () => {
    BackgroundService.stop();
  };
  export default {
    start,
    stop,
  };
// App.js
BackgroundTaskService.start();
1 Answers

Are you testing on a physical device and have enabled Background App Refresh in the Xcode settings, as well as on your physical device? According to documentation in another package, React-native-background-task indicates that that package must be tested on a physical device, or else the iOS simulator version will have to emulate the tasks.

I attempted to rework your code. I am currently getting this to work with BackgroundService instead of BackgroundJob for running in the background of the app. I am also working on testing when the app is closed. Should be able to get back to you soon.

import BackgroundService from 'react-native-background-actions';
import AsyncStorage from "@react-native-async-storage/async-storage";
import BackgroundJob from 'react-native-background-actions';
import {Alert} from "react-native";

const sleep = (time) => new Promise((resolve) => setTimeout(() => resolve(), time));

// You can do anything in your task such as network requests, timers and so on,
// as long as it doesn't touch UI. Once your task completes (i.e. the promise is resolved),
// React Native will go into "paused" mode (unless there are other tasks running,
// or there is a foreground app).
const veryIntensiveTask = async (taskDataArguments) => {
  // Example of an infinite loop task
  const { delay } = taskDataArguments;
  await new Promise( async (resolve) => {
    for (let i = 0; BackgroundService.isRunning(); i++) {
      console.log(i);
      console.log("Running background task");
      Alert.alert("Running background task");
      await sleep(delay);
    }

    // for (let i = 0; BackgroundJob.isRunning(); i++) {
    //
    //   var value = await AsyncStorage.getItem('COUNT');
    //   if(!value) {
    //     await AsyncStorage.setItem('COUNT', "2");
    //   }
    //   var value = await AsyncStorage.getItem('COUNT');
    //   await AsyncStorage.setItem('COUNT', (parseInt(value) + 1).toString());
    //   value = await AsyncStorage.getItem('COUNT');
    //   console.log('value', value);
    //   await sleep(delay);
    //
    // }

  });
};

const options = {
  taskName: 'Example',
  taskTitle: 'ExampleTask title',
  taskDesc: 'ExampleTask description',
  taskIcon: {
    name: 'ic_launcher',
    type: 'mipmap',
  },
  color: '#ff00ff',
  linkingURI: 'yourSchemeHere://chat/jane', // See Deep Linking for more info
  parameters: {
    delay: 10000,
  },
};


const start = async () => {
  return BackgroundService.start(veryIntensiveTask, options);
};
const stop = async () => {
  return BackgroundService.stop();
};
export default {
  start,
  stop,
};

// await BackgroundService.start(veryIntensiveTask, options);
// await BackgroundService.updateNotification({taskDesc: 'New ExampleTask description'}); // Only Android, iOS will ignore this call
// // iOS will also run everything here in the background until .stop() is called
// await BackgroundService.stop();
Related