capacitor-community / background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
MIT License
177 stars 54 forks source link

App crashes randomly #86

Closed PuscasAdi closed 1 year ago

PuscasAdi commented 1 year ago

Describe the bug Our clients complain that the app crashes randomly. Also we have crash logs about this plugin regarding the crashes

To Reproduce Steps to reproduce the behavior:

  1. There are no exact steps to reproduce

Expected behavior App should not crash randomly

Screenshots image

Smartphone:

Additional context com.equimaps.capacitor_background_geolocation.BackgroundGeolocationService$LocalBinder.onActivityStopped android.app.ForegroundServiceStartNotAllowedException

Type java.lang.RuntimeException Exception java.lang.RuntimeException: at android.app.ActivityThread.performPauseActivityIfNeeded (ActivityThread.java:5159) at android.app.ActivityThread.performPauseActivity (ActivityThread.java:5110) at android.app.ActivityThread.handlePauseActivity (ActivityThread.java:5062) at android.app.servertransaction.TransactionExecutor.performLifecycleSequence (TransactionExecutor.java:229) at android.app.servertransaction.TransactionExecutor.cycleToPath (TransactionExecutor.java:201) at android.app.servertransaction.TransactionExecutor.executeLifecycleState (TransactionExecutor.java:173) at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:97) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2259) at android.os.Handler.dispatchMessage (Handler.java:106) at android.os.Looper.loopOnce (Looper.java:210) at android.os.Looper.loop (Looper.java:299) at android.app.ActivityThread.main (ActivityThread.java:8103) at java.lang.reflect.Method.invoke at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:556) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1045) Caused by android.app.ForegroundServiceStartNotAllowedException: at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:54) at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:50) at android.os.Parcel.readParcelable (Parcel.java:3334) at android.os.Parcel.createExceptionOrNull (Parcel.java:2421) at android.os.Parcel.createException (Parcel.java:2410) at android.os.Parcel.readException (Parcel.java:2393) at android.os.Parcel.readException (Parcel.java:2335) at android.app.IActivityManager$Stub$Proxy.setServiceForeground (IActivityManager.java:7288) at android.app.Service.startForeground (Service.java:733) at com.equimaps.capacitor_background_geolocation.BackgroundGeolocationService$LocalBinder.onActivityStopped (BackgroundGeolocationService.java:146) at com.equimaps.capacitor_background_geolocation.BackgroundGeolocation.handleOnPause (BackgroundGeolocation.java:325) at com.getcapacitor.Bridge.onPause (Bridge.java:1221) at com.getcapacitor.BridgeActivity.onPause (BridgeActivity.java:93) at android.app.Activity.performPause (Activity.java:8449) at android.app.Instrumentation.callActivityOnPause (Instrumentation.java:1530) at android.app.ActivityThread.performPauseActivityIfNeeded (ActivityThread.java:5149) Caused by android.os.RemoteException: Remote stack trace: at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked (ActiveServices.java:1859) at com.android.server.am.ActiveServices.setServiceForegroundLocked (ActiveServices.java:1368) at com.android.server.am.ActivityManagerService.setServiceForeground (ActivityManagerService.java:12227) at android.app.IActivityManager$Stub.onTransact (IActivityManager.java:3293) at com.android.server.am.ActivityManagerService.onTransact (ActivityManagerService.java:2559)

diachedelic commented 1 year ago

From the stacktrace, it appears that this is where the exception is thrown: https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/am/ActiveServices.java#1962. But why?

diachedelic commented 1 year ago

This looks similar to this: https://stackoverflow.com/questions/70711950/android-12-foregroundservicestartnotallowedexception-while-in-foreground.

The exception is thrown when the plugin calls Service.startForeground, and this occurs during the Activity.onPause lifecycle hook. So at that point, the app should well and truly be in the foreground, not the background. So it is mysterious that this exception would be thrown.

diachedelic commented 1 year ago

It seems this may be a bug in Android 12 (perhaps only for Samsung devices?). There is an Android bug report here that looks related: https://issuetracker.google.com/issues/229000935#comment35

Possible solutions:

jamesdiacono commented 1 year ago

This should be fixed as of v1.2.7.