capacitor-community / background-geolocation

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

App crashes on Android 12 (SDK 31) devices #49

Closed ericaig closed 2 years ago

ericaig commented 2 years ago

Describe the bug The app crashes after calling the BackgroundGeolocation.addWatcher.

To Reproduce Steps to reproduce the behavior:

  1. Simply use this plugin on a device that runs on Android 12 (SDK 31).
  2. The app will automatically crash after calling BackgroundGeolocation.addWatcher and gives the following error:

    Error Message

    2022-02-02 09:18:34.185 4220-4289/com.bundleid.example E/Capacitor: Serious error executing plugin java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:121) at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:592) at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.os.HandlerThread.run(HandlerThread.java:67) Caused by: java.lang.IllegalArgumentException: com.bundleid.example: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles. at android.app.PendingIntent.checkFlags(PendingIntent.java:375) at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:458) at android.app.PendingIntent.getActivity(PendingIntent.java:444) at android.app.PendingIntent.getActivity(PendingIntent.java:408) at com.equimaps.capacitor_background_geolocation.BackgroundGeolocation.addWatcher(BackgroundGeolocation.java:121) ... 9 more --------- beginning of crash 2022-02-02 09:18:34.200 4220-4289/com.bundleid.example E/AndroidRuntime: FATAL EXCEPTION: CapacitorPlugins Process: com.bundleid.example, PID: 4220 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:601) at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.os.HandlerThread.run(HandlerThread.java:67) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:121) at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:592) at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8)  at android.os.Handler.handleCallback(Handler.java:938)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loopOnce(Looper.java:201)  at android.os.Looper.loop(Looper.java:288)  at android.os.HandlerThread.run(HandlerThread.java:67)  Caused by: java.lang.IllegalArgumentException: com.bundleid.example: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles. at android.app.PendingIntent.checkFlags(PendingIntent.java:375) at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:458) at android.app.PendingIntent.getActivity(PendingIntent.java:444) at android.app.PendingIntent.getActivity(PendingIntent.java:408) at com.equimaps.capacitor_background_geolocation.BackgroundGeolocation.addWatcher(BackgroundGeolocation.java:121) ... 9 more

Expected behavior It should successfully get the current GPS location data

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional information Using a simulator with Android 12 also crashes

Simulator specs

Name: Samsung-FMAP_API_31 CPU/ABI: Intel Atom (x86_64) Path: /Users/ericaig/.android/avd/Samsung-FMAP_API_31.avd Target: default [Default Android System Image] (API level 31) Skin: 1080x2400 fastboot.chosenSnapshotFile: runtime.network.speed: full hw.accelerometer: yes hw.device.name: Samsung-FMAP hw.lcd.width: 1080 hw.initialOrientation: Portrait image.androidVersion.api: 31 tag.id: default hw.mainKeys: no hw.camera.front: emulated avd.ini.displayname: Samsung-FMAP API 31 hw.gpu.mode: auto hw.ramSize: 1536 PlayStore.enabled: false fastboot.forceColdBoot: no hw.cpu.ncore: 4 hw.keyboard: yes hw.sensors.proximity: yes hw.dPad: no hw.lcd.height: 2400 vm.heapSize: 256 skin.dynamic: yes hw.device.manufacturer: User hw.gps: yes skin.path.backup: _no_skin hw.audioInput: yes image.sysdir.1: system-images/android-31/default/x86_64/ showDeviceFrame: no hw.camera.back: virtualscene AvdId: Samsung-FMAP_API_31 hw.lcd.density: 480 hw.arc: false hw.device.hash2: MD5:--- fastboot.forceChosenSnapshotBoot: no fastboot.forceFastBoot: yes hw.trackBall: no hw.battery: yes hw.sdCard: yes tag.display: Default Android System Image runtime.network.latency: none disk.dataPartition.size: 800M hw.sensors.orientation: yes avd.ini.encoding: UTF-8 hw.gpu.enabled: yes

diachedelic commented 2 years ago

Please change line 125 of BackgroundGeolocation.java to PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE and let me know if that solves the problem.

ericaig commented 2 years ago

It works! It works on the Simulator and the physical Samsung device. Thanks a lot. I'm going to close this issue now.

diachedelic commented 2 years ago

This should be fixed in v1.0.5 and v0.3.13. Please test.