HaylLtd / cordova-background-geolocation-plugin

Background and foreground geolocation plugin for Cordova.
Apache License 2.0
56 stars 64 forks source link

Fixed onCreate issue Android 14 #178

Closed rrrasti closed 4 months ago

rrrasti commented 7 months ago
  1. Added PendingIntent.FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT for PendingIntent objects created in DistanceFilterLocationProvider and ActivityRecognitionLocationProvider.

This solves the following error when targeting Android 14+:

java.lang.IllegalArgumentException: Targeting U+ (version 34 and above) disallows creating or retrieving a PendingIntent with FLAG_MUTABLE, an implicit Intent within and without FLAG_NO_CREATE and FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT for security reasons. To retrieve an already existing PendingIntent, use FLAG_NO_CREATE, however, to create a new PendingIntent with an implicit Intent use FLAG_IMMUTABLE.

  1. Added RECEIVER_NOT_EXPORTED to registerReceiver.

This solves the following error when targeting Android 14+:

java.lang.SecurityException: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts

HarelM commented 7 months ago

How sure are you about this fix? The following is similar I believe. https://github.com/HaylLtd/cordova-background-geolocation-plugin/pull/99

rrrasti commented 7 months ago
HarelM commented 7 months ago

Do you think you can implement the "better" solution?

rrrasti commented 7 months ago

I refactored code to use explicit intents instead of implicit ones.

HarelM commented 7 months ago

Great, thanks! Can you confirm this code is working for previous versions? mainly Android 13 and maybe 12?

HarelM commented 4 months ago

@rrrasti I want to release a version soon, any chance to get a feedback on the above questions?

brunochikuji commented 4 months ago

Hi @HarelM, do you have any plans to release the new version with this correction?

I'm having problems with the plugin when compiling for Android SDK 34, and testing the code in this PR worked correctly

HarelM commented 4 months ago

I need answers to the above questions (android 12, 13 and mutable flag) in order to merge this. Once merged I'll release a new version. If you can take a look at the above and answer I'll be happy to merge.

brunochikuji commented 4 months ago

Hi @HarelM. I did 2 tests here on Android 14 (Physical) and Android 10 (Virtual):

1 - Compiled for SDK 34: Ok, capture working in the background 2 - Compiled for SDK 33: Ok, capture working in the background

My config: `{ // all activityType: ios ? 'Other' : 'OtherNavigation', locationProvider: BackgroundGeolocationLocationProvider.RAW_PROVIDER, desiredAccuracy: 10, stationaryRadius: ios ? 5 : 1, distanceFilter: ios ? 5 : 1, debug: false, stopOnTerminate: true,

  startForeground: true,
  interval: 300000, // 5 minutes
  fastestInterval: 300000, // 5 minutes
  activitiesInterval: 300000, // 5 minutes
  notificationsEnabled: false,

  // iOS
  saveBatteryOnBackground: true,
};`

Do you need any more tests? And without these adjustments when compiling for SDK 34 the event to update the geolocation is not fired in the background

HarelM commented 4 months ago

Ahh, I thought that there was a change around the mutable flags, but it was only a white space change... I'll push this forward within the next few days.

brunochikuji commented 4 months ago

@HarelM Ok I will wait

HarelM commented 4 months ago

I have released a new version, and tested it on my app only to see that the locations are not captured while the app is not topmost... :-( I'll need to investigate I guess if this fix works for you...

brunochikuji commented 4 months ago

@HarelM

I tested it today with the new version and when leaving the application in the background, geolocations continued to be captured. I use sending requests with the capacitor http to continue being sent after 5 minutes

Which SDK target did you test? My SDK target and compile is 34 and is required add this permission in AndroidManifest: <uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>

HarelM commented 4 months ago

I released a new version with foreground service location permission yesterday, which is working as expected on my physical device and on the emulator. But it didn't work without the above permission...