Skip to content

S.O Kill Foreground Service. #250

@Beckmann0o

Description

@Beckmann0o

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 !

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions