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

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

Open
danchiche opened this issue Jun 17, 2024 · 10 comments
Open

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

danchiche opened this issue Jun 17, 2024 · 10 comments

Comments

@danchiche
Copy link

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

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

@danchiche
Copy link
Author

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

Are notifications allowed in the Android app settings?

@danchiche
Copy link
Author

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

danchiche commented Jun 18, 2024

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

jacopo-ferroni commented Jun 19, 2024

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

There are some observation:

  • foregroundServiceType=shortService working on Android 14 only 15 minutes according to developer.android.com
  • When I tried change to location it works fine on Android 14, but on Android 10 this service has unexpected behavior (e.g. it can work as 15 minutes or 8 hours) P.S. Maybe it just problem with Xiaomi

@vzkharov
Copy link

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

@Dwikavindra
Copy link

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.

@tmaly1980
Copy link

tmaly1980 commented Dec 5, 2024

@Dwikavindra I believe you are onto something! I wonder if foregroundServiceType=specialUse would work for longer tasks?

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

6 participants