react-native-foreground-service icon indicating copy to clipboard operation
react-native-foreground-service copied to clipboard

Multiple instances getting created

Open veris-NikhilSrivastava opened this issue 4 years ago • 3 comments

Hi, So I was using a notifications library which has a function which detects that whether app is opened via notification or not (onNotificationOpened()). So, every time a foreground service is created, and the app is closed and if we tap on notification then the onNotificationOpened() gets called as many times as the app is closed and opened when the foreground service is started. Seems like multiple instances are getting created. I've also set the android:launchMode="singleTop" in AndroidManifest.xml

Please if any one could throw some light on this.

Thanks

veris-NikhilSrivastava avatar Jun 09 '20 14:06 veris-NikhilSrivastava

+1

tal32123 avatar Sep 16 '20 12:09 tal32123

Samething is happening here. After close the app and reopen it, it generates two instances, if I close it again and reopen, it generates more one instance and it goes on until I get a java.lang.OutOfMemoryError. Any ideas?

mathBordin avatar Nov 05 '20 21:11 mathBordin

Because there is a bug in the module its not listening to destroy events of the app we can implement a module to listen and kill app or stop the service by that.

LifeCyleModule.java

import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class lifeCycleModule extends ReactContextBaseJavaModule implements LifecycleEventListener {

    private static ReactApplicationContext reactContext;

    lifeCycleModule(ReactApplicationContext context) {
        super(context);
        reactContext = context;
        reactContext.addLifecycleEventListener(this);
    }

    @Override
    public String getName() {
        return "lifeCycleModule";
    }

    @Override
    public void onHostResume() {
        // Activity `onResume`
    }

    @Override
    public void onHostPause() {
        // Activity `onPause`
    }

    @Override
    public void onHostDestroy() {
        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit("DESTROY",null);
    }
    @ReactMethod
    public void kill(){
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

}

LifeCylePackage.java

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class lifeCyclePackage implements ReactPackage {

    public lifeCyclePackage() {
    }

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();
        modules.add(new lifeCycleModule(reactContext));
        return modules;
    }

    public List<Class<? extends JavaScriptModule>> createJSModules() {
        return Collections.emptyList();
    }

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }
}
import {
    NativeModules,
    NativeEventEmitter,
} from 'react-native';

const {lifeCycleModule} = NativeModules;

const eventEmitter = new NativeEventEmitter(lifeCycleModule);
        eventEmitter.addListener('DESTROY', (event) => {
            try{
                VIForegroundService.stopService();
                lifeCycleModule.kill();
            }catch (e) {
                console.log(e);
            }
        });

esinanturan avatar Feb 12 '21 22:02 esinanturan