Skip to content

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
implementation 'com.pushwoosh:pushwoosh-location:6.+'

To launch the module, use the startLocationTracking method:

PushwooshLocation.startLocationTracking();

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:

...
<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

For updating the geolocation in the background with no ForegroundService launching, you will need the following:

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

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:

PushwooshLocation.requestBackgroundLocationPermission();

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

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.