voximplant / react-native-foreground-service

React native module to start foreground service on android
MIT License
159 stars 91 forks source link

Multiple instances getting created #28

Open veris-NikhilSrivastava opened 4 years ago

veris-NikhilSrivastava commented 4 years ago

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

tal32123 commented 4 years ago

+1

mathBordin commented 4 years ago

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?

esinanturan commented 3 years ago

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