Rapsssito / react-native-background-actions

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

It runs tasks in background or even app is closed only for max 30 secs #235

Open AyyazAnjum opened 3 months ago

AyyazAnjum commented 3 months ago

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...

MalekZishan commented 3 months ago

Yes same i am facing now

rajkumarthirumalai commented 3 months ago

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

AyyazAnjum commented 3 months ago

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

rajkumarthirumalai commented 3 months ago

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>
deban07 commented 3 months ago

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" />
anurastogi commented 3 months ago

The answer by deban07 and rajkumarthirumalai worked perfectly with version 4.0.1

vishalyad16 commented 2 months ago

@deban07 @AyyazAnjum @deban07 @rajkumarthirumalai @MalekZishan still crashing on my side

zouinekh commented 2 months ago

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 commented 2 months ago

@deban07 @AyyazAnjum @deban07 @rajkumarthirumalai @MalekZishan still crashing on my side

Which android version you are using and mention the React Native version also.

rajkumarthirumalai commented 2 months ago

@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);
}

}`

  1. 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();`

Ezzine-Smichi commented 1 month ago

i am facing this bug now in the ios platform, there is a solution for this or not yet