Rapsssito / react-native-background-actions

React Native background service library for running background tasks forever in Android & iOS.
MIT License
818 stars 117 forks source link

S.O Kill Foreground Service. #250

Open Beckmann0o opened 2 weeks ago

Beckmann0o commented 2 weeks ago

Hello Guys, good morning. Implement Background Actions to perform a task by calling a native module that scans BLE devices, if I use the cell phone frequently the foregroundservice runs perfectly throughout the day. The problem comes when the screen turns off for several hours, I find that the OS kills the service since I don't see it in the silent notifications. Activate without restrictions to save battery in my app, also in the foregroundservice type I configure it as follows: Option for notification: <service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask" android:foregroundServiceType="connectedDevice|dataSync|location">

I also went on to configure the notification and the task performed below: O `const options = { taskName: 'BLEScanner', taskTitle: 'Escáner BLE Activo', taskDesc: 'Buscando dispositivos W6...', taskIcon: { name: 'ic_launcher', type: 'mipmap', }, color: '#FF0000', parameters: { delay: 15000, },

importance: 4, // IMPORTANCE_HIGH
notification: {
  channelId: 'ble-scanner',
  channelName: 'Escáner BLE',
  channelDescription: 'Servicio crítico de escaneo de dispositivos',
  channelImportance: 4, // IMPORTANCE_HIGH
  visibility: 'public',
  ongoing: true,
  priority: 'high',
  android: {
    foregroundServiceTypes: [
      'connectedDevice',
      'dataSync',
      'location'
    ]
  }
}

};`

Task configuration:: `const sleep = (time) => new Promise((resolve) => setTimeout(() => resolve(), time));

const veryIntensiveTask = async (taskDataArguments) => { console.log("Iniciando tarea de escaneo en segundo plano"); let lastPanicSendTime: Date = null;

await new Promise(async (resolve) => {
  while (BackgroundService.isRunning()) {
    try {
      await BackgroundService.updateNotification({
        taskDesc: "Escaneando dispositivos W6...",
      });

      if (
        lastPanicSendTime == null ||
        new Date().getTime() - lastPanicSendTime.getTime() > 60000
      ) {
        const devices = await CustomModule.scanDevices();
        //SI ENCONTRO UN PAQUETE
        if (devices.length > 0) {
          lastPanicSendTime = new Date();
          try {
            const locationNativo = await CustomModule.getCurrentLocation();
            const locationToSv = formatPosition(locationNativo);
            positionStore.sendPosition([locationToSv]);
          } catch (error) {
            console.error("Error al enviar la posición:", error);
            await BackgroundService.updateNotification({
              taskDesc: `Error al enviar la posición: ${error.message}`,
            });
          }
        } else {
          await BackgroundService.updateNotification({
            taskDesc: `No se encontraron dispositivos W6`,
          });
        }
      }
      await sleep(SCAN_INTERVAL);
    } catch (error) {
      console.error("Error en el ciclo de escaneo:", error);
      await sleep(SCAN_INTERVAL);
    }
  }
});

}; ` Is there any specific configuration or anything I can change to make my foregroundservice more reliable and resilient to being killed by the operating system when the screen is off for several hours? THANK YOU SO MUCH !

pankuweb commented 1 day ago

I'm also getting same issue when use android:foregroundServiceType="shortService" then it works only 3 minutes when use android:foregroundServiceType="dataSync" then max 40 minutes

Is there any way it should work two or more days without crashing.