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
Anchor link toIf 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:
implementation 'com.pushwoosh:pushwoosh-location:6.+'To launch the module, use the startLocationTracking method:
PushwooshLocation.startLocationTracking();PushwooshLocation.startLocationTracking()Using ForegroundService to make location requests when your application is in background
Anchor link toTo 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:
...<application>....    <meta-data        android:name="com.pushwoosh.start_foreground_service"        android:value="true" />...</application>...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:
...<application>....    <meta-data        android:name="com.pushwoosh.foreground_service_notification_text"        android:value="My notification text" />    <meta-data        android:name="com.pushwoosh.foreground_service_notification_channel_name"        android:value="My notification channel name" />...</application>...Background location access
Anchor link toFor 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:
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />- 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_LOCATIONuser-permission is present in the app’s AndroidManifest.xml.
Call the method as follows:
PushwooshLocation.requestBackgroundLocationPermission();PushwooshLocation.requestBackgroundLocationPermission()Android 12+ permission types
Anchor link toStarting 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
Anchor link toYour 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.