Is it possible to detect and do an action when user closes an Android/IOS Cordova/Capacitor App?

Viewed 426

I'm developing an App with Capacitor and Quasar Framework. I wanted the app to log and close any pending notification from the app when the user closes the app(not background, really closing it by mobile interface like swiping right on "opened apps menu". I'm using background to track geolocation, so "Terminate" event is not really when the app closes.

I tried using the window.addEventListener("beforeunload") and I developed a plugin in Java that tried to throw an event to a listener: Runtime.getRuntime().addShutdownHook, neither helped me handle that.

Is that something possible? I wanted to log the closing and check if there is a notification still showing to the user, so I can close it.

Capacitor Plugin code

public void load() {
    Runtime.getRuntime().addShutdownHook(new Thread() {

      @Override
      public void run() {
        System.out.println("appClosed");
        JSObject ret = new JSObject();
        ret.put("appClosed", true);
        notifyListeners("appClosed", ret);
      }

    });
}
0 Answers
Related