Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S.O Kill Foreground Service. #250

Open
Beckmann0o opened this issue Nov 6, 2024 · 3 comments
Open

S.O Kill Foreground Service. #250

Beckmann0o opened this issue Nov 6, 2024 · 3 comments

Comments

@Beckmann0o
Copy link

Beckmann0o commented Nov 6, 2024

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
Copy link

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.

@keonfloww
Copy link

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.

im have save issue. need help

@smitha-2020
Copy link

@Beckmann0o where you able to solve it? Whats the version of react native and react-native-background-actions you are using. I have issue registered with background on android 14 at #251 . it crashes almost immediately when background task starts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants