Using Pushwoosh location module in Android apps
Please be aware that geo-based messaging is not available for HMS (Huawei Mobile Services) devices as it's built upon Google location services.
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.
If you’re going to use the Pushwoosh location module to update user’s location only when your app is active, add the
pushwoosh-locationmodule to your app/build.gradle:
Get the latest module version in Pushwoosh Github repo: https://github.com/Pushwoosh/pushwoosh-android-sdk/releases
To launch the module, use the
pushwoosh-locationmodule will request all the necessary permissions from a user (ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION).
Due to Android 12 updates, ForegroundService cannot perform correctly, so it's deprecated and will be removed from Pushwoosh SDK further.
Here are some workarounds for tracking users' location in background:
- 1.Use targetSdkVersion 30 instead of 31. Please keep in mind that features present in Android 12 will not be available in that case.
- 2.Ask your users to disable battery optimization for your app to let the ForegroundService start in background.
To launch the ForegroundService when the
startLocationTrackingmethod 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:
android:value="My notification text" />
android:value="My notification channel name" />
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:
<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-locationmodule 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.
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:
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.
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.