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:
build.gradle
1
implementation 'com.pushwoosh:pushwoosh-location:6.+'
Copied!
Get the latest module version in Pushwoosh Github repo: https://github.com/Pushwoosh/pushwoosh-android-sdk/releases
To launch the module, use the startLocationTracking method:
Java
Kotlin
1
PushwooshLocation.startLocationTracking();
Copied!
1
PushwooshLocation.startLocationTracking()
Copied!
The pushwoosh-location module will request all the necessary permissions from a user (ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION).

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:
1
...
2
<application>
3
....
4
<meta-data
5
android:name="com.pushwoosh.start_foreground_service"
6
android:value="true" />
7
...
8
</application>
9
...
Copied!
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:
1
...
2
<application>
3
....
4
<meta-data
5
android:name="com.pushwoosh.foreground_service_notification_text"
6
android:value="My notification text" />
7
<meta-data
8
android:name="com.pushwoosh.foreground_service_notification_channel_name"
9
android:value="My notification channel name" />
10
...
11
</application>
12
...
Copied!

Background location access

For updating the geolocation in the background with no ForegroundService launching, you will need the following:
1
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
Copied!
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.
Please make sure to clearly explain to users for what reasons and purposes your app needs access to their location in the background. Refer to the Android developer guides for details.
Call the method as follows:
Java
Kotlin
1
PushwooshLocation.requestBackgroundLocationPermission();
Copied!
1
PushwooshLocation.requestBackgroundLocationPermission()
Copied!

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:
    1.
    Approximate location, that is equal to ACCESS_COARSE_LOCATION
    2.
    Precise location, that is equal to ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION
Please take into account that location requests from applications with Manifest.permission.ACCESS_COARSE_LOCATION and not Manifest.permission.ACCESS_FINE_LOCATION will be automatically throttled to a slower interval, and the location object will be obfuscated to only show a coarse level of accuracy. Refer to https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest for details.
Keep in mind that the level of precision depends on which permission you request:
    ACCESS_COARSE_LOCATION – Provides an estimate of the device's location, to within about 1 mile (1.6 km).
    ACCESS_FINE_LOCATION – Provides an estimate of the device's location that is as accurate as possible, usually within about 160 feet (50 meters) and sometimes as accurate as within 10 feet (a few meters) or better.
Last modified 1mo ago