Rapsssito / react-native-background-actions

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

Android 14 (Samsung A14) : service stop after backgrounded it #225

Open danchiche opened 2 months ago

danchiche commented 2 months ago

Hi, For testing I start a counter when I click to a button on the app. When I close the app (put it in background), the counter freeze. When I switch off the screen, and switch back on, the counter continue when expected.

This problem is constated on Samsung A14, but I think is due to Android 14+. I have a samsung s9 on android 10 and no problem, when I close the app, counter continue normally.

I tested this on expo 49 with react-native-background-actions 3.0.1 and with expo 51 with react-native-background-actions 4.0.0, and both the same experience.

Do anyone know what can I do ?

Thanks

j-jonathan commented 2 months ago

Hi, The version 4.0.0 has just been released with support for Android 14 / SDK 34 :)

danchiche commented 2 months ago

Yes but like I said before, it's not a problem of react-native-background-actions 4.0.0 I tested with expo 49 with react-native-background-actions 3.0.1, and the same problem

j-jonathan commented 2 months ago

Are notifications allowed in the Android app settings?

danchiche commented 2 months ago

It's impossible to enable notification in my app on the samsung A14, other apps have the ability to enable it. I try to find on the internet how to enable notifications on my app, but I don't have the restricted setting option. On my samsung s9 notification is allowed, and I see the notification when there is a task in background.

danchiche commented 2 months ago

New update : I ask for notification permission (programmaticaly) at app startup, and now I am able to allow the permission on the A14. I make a new test and it's the same bug : the counter stop 5 seconds after I close the app, and continue when I open it again (or I switch off and on the screen)

jacopo-ferroni commented 2 months ago

Help, same issue! Furthermore, after an uncertain time the app shows "the app is not responding" whit two choices "close app" or "wait".

This is my AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>

  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />

  <uses-feature android:name="android.hardware.camera" />
  <uses-permission android:name="android.permission.CAMERA"/>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  <uses-permission android:name="android.permission.VIBRATE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <queries>
    <intent>
      <action android:name="android.intent.action.VIEW"/>
      <category android:name="android.intent.category.BROWSABLE"/>
      <data android:scheme="https"/>
    </intent>
  </queries>
  <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="true" android:theme="@style/AppTheme">
    <meta-data android:name="expo.modules.updates.ENABLED" android:value="false"/>
    <meta-data android:name="expo.modules.updates.EXPO_UPDATES_CHECK_ON_LAUNCH" android:value="ALWAYS"/>
    <meta-data android:name="expo.modules.updates.EXPO_UPDATES_LAUNCH_WAIT_MS" android:value="0"/>
    <activity android:name=".MainActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|uiMode" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize" android:theme="@style/Theme.App.SplashScreen" android:exported="true" android:screenOrientation="portrait">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="com.anonymous.ssensemobileapp"/>
      </intent-filter>
    </activity>
    <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" android:exported="false"/>
    <service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask" android:foregroundServiceType="shortService"/>
    <service android:name=".MyService" android:foregroundServiceType="location" android:exported="false" android:enabled="true" android:stopWithTask="false"/>
  </application>
</manifest>

and this my snippet:

import BackgroundService from 'react-native-background-actions';

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

const printer = async (taskDataArguments) => {
    // Esempio di un task in loop infinito
    const { delay } = taskDataArguments;
    await new Promise(async (resolve) => {
        for (let i = 0; BackgroundService.isRunning(); i++) {
            console.log(i);
            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: 2000,
    },
};

BackgroundService.start(printer, options);
vzkharov commented 2 months ago

There are some observation:

vzkharov commented 2 months ago

Maybe there are some assumptions how to manage stable work of this background-task?

Dwikavindra commented 3 weeks ago

Maybe there are some assumptions how to manage stable work of this background-task?

If you look at the official docs fgservicestypes

shortService only runs for 3 minutes. I think it has something to do with that it's not supposed to run longer than that thus causing errors.