Using Pushwoosh location module in Android apps
There are two location access categories in Android:
- Foreground location access
- Background location access
For updating the user’s location in the foreground, one of the following conditions should be met:
- One of the app’s activities is active
- Foreground service of foregroundServiceType = “location” is launched for the app
Background access allows the app to get the user’s geolocation while the app is inactive, and no foreground services of the “location” type are launched for that app.
Foreground location access
If you’re going to use the Pushwoosh location module to update user’s location only when your app is active, add the pushwoosh-location
module to your app/build.gradle:
To launch the module, use the startLocationTracking
method:
Using ForegroundService to make location requests when your application is in background
To launch the ForegroundService when the startLocationTracking
method is called, which allows to get the user’s geolocation when your application is in the background, follow the steps described in the previous paragraph, then add the following meta-data to your AndroidManifest.xml:
When ForegroundService is launched, a notification is being created. You can customize the notification by specifying the additional meta-data in your project’s AndroidManifest.xml:
Background location access
For updating the geolocation in the background with no ForegroundService launching, you will need the following:
- Get the approval from Google Play for your app’s access to location in the background (refer to https://support.google.com/googleplay/android-developer/answer/9799150?hl=en for details)
- Add the permission to your AndroidManifest.xml:
- Request user’s permission on updating location in the background after the foreground location permission is received (find the guidelines in the Android developer documentation: https://developer.android.com/training/location/permissions#background-dialog-target-android-11 )
In the 6.3.2 pushwoosh-location
module version, we included an additional method that opens the location permission settings screen if the following conditions are met:
- There is the foreground location permission granted for the app;
ACCESS_BACKGROUND_LOCATION
user-permission is present in the app’s AndroidManifest.xml.
Call the method as follows:
Android 12+ permission types
Starting from Android 12, when being asked for foreground location access permission, a user can choose how often and with what precision the app will get the user’s location.
There are two types of that permissions:
- Approximate location, that is equal to ACCESS_COARSE_LOCATION
- Precise location, that is equal to ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION
Share your feedback with us
Your feedback helps us create a better experience, so we would love to hear from you if you have any issues during the SDK integration process. If you face any difficulties, please do not hesitate to share your thoughts with us via this form.