It runs tasks in background or even app is closed only for max 30 secs
AyyazAnjum opened this issue ยท 11 comments
When my app is in background or is killed from background my function runs for only short period of time like 10 to 20 secs only for android 12+
but for android below 12+ it is working fine...
Yes same i am facing now
yes facing the same even manually changing service type to dataSync it works properly for android 14 version but not on android lower version device
the app get freeze mode i think so
2024-08-05 17:54:15.093 2098-3020 ColorHansManager system_serverIpkg:com.myApp,state: M -> F
2024-08-05 17:54:15.098 2098-3020 ColorHansManager system_serverIfreeze uid: 10322 package:com.myApp reason: AsyncBinder scene: LcdOnScene
yes facing the same even manually changing service type to dataSync it works properly for android 14 version but not on android lower version device
What your code is working for latest Android and not for lower devices
How?
I'm facing the same issue but vice versa
Working on lower devices but not on latest
For this you have to
1.npm i react-native-background-actions@4.0.1
2.change the nodemodules
node_modules/react-native-background-actions/android/src/main/AndroidManifest.xml
add this in service : android:foregroundServiceType="dataSync"
EXISTING CODE
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.asterinet.react.bgactions">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application>
<service android:name=".RNBackgroundActionsTask"/>
</application>
</manifest>
UPDATED ONE
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.asterinet.react.bgactions">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application>
<service android:name=".RNBackgroundActionsTask" android:foregroundServiceType="dataSync" />
</application>
</manifest>
When my app is in background or is killed from background my function runs for only short period of time like 10 to 20 secs only for android 12+ but for android below 12+ it is working fine...
It will work for all android devices .
Add these at => android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
The answer by deban07 and rajkumarthirumalai worked perfectly with version 4.0.1
@deban07 @AyyazAnjum @deban07 @rajkumarthirumalai @MalekZishan still crashing on my side
When my app is in background or is killed from background my function runs for only short period of time like 10 to 20 secs only for android 12+ but for android below 12+ it is working fine...
It will work for all android devices . Add these at => android/app/src/main/AndroidManifest.xml <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
wlh mak mnayk sa7by it works 3alkher
@deban07 @AyyazAnjum @deban07 @rajkumarthirumalai @MalekZishan still crashing on my side
Which android version you are using and mention the React Native version also.
@deban07 i have stopped using this package usage and used native foreground service from android
1.create a java serviceFile
`package com.yourpackage.service;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import androidx.core.app.NotificationCompat;
public class BackgroundService extends Service {
private static final int NOTIFICATION_ID = 1;
private static final String CHANNEL_ID = "BackgroundServiceChannel";
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(NOTIFICATION_ID, createNotification());
// Your long-running task goes here
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Background Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
private Notification createNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Background Service")
.setContentText("Service is running in the background")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
return builder.build();
}
}`
2.Add this manifest
<service android:name=".BackgroundService" android:foregroundServiceType="dataSync" android:enabled="true" android:exported="false" />
3.now comes the module
`package com.yourpackage.module;
import android.content.Intent;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.yourpackage.service.BackgroundService;
public class BackgroundServiceModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
public BackgroundServiceModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return "BackgroundService";
}
@ReactMethod
public void startService() {
Intent serviceIntent = new Intent(reactContext, BackgroundService.class);
reactContext.startService(serviceIntent);
}
@ReactMethod
public void stopService() {
Intent serviceIntent = new Intent(reactContext, BackgroundService.class);
reactContext.stopService(serviceIntent);
}
}`
- need to add the package to module
`package com.yourpackage;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.yourpackage.module.BackgroundServiceModule;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class BackgroundServicePackage implements ReactPackage {
@OverRide
public List createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new BackgroundServiceModule(reactContext));
return modules;
}
}`
5.Finally at our jsx :
`import { NativeModules } from 'react-native';
const { BackgroundService } = NativeModules;
// Start the service
BackgroundService.startService();
// Stop the service
BackgroundService.stopService();`
i am facing this bug now in the ios platform, there is a solution for this or not yet