Install SDK on Android
Add Hypertrack SDK
Add following lines to your applications build.gradle
:
note
We constantly work on making our SDKs better, so make sure you have the latest version of it. You might take a look of its changelog here.
Set up silent push notifications
Set up silent push notifications to manage on-device tracking using HyperTrack cloud APIs from your server. This requires Firebase push notification. If you do not yet have push notifications enabled, please proceed to setup Firebase Cloud Messaging.
Also you need to add your Firebase API key to your HyperTrack Dashboard Setup Page under Server to Device communication section.
note
Push notifications have delays so if you're looking for more instant channel you can use syncDeviceSettings
sdk method to speed up command propagation.
Initialize SDK
Obtain an SDK instance, when you wish to use SDK, by passing your publishable key from the Setup page.
- Java
- Kotlin
Also make sure you've requested permissions somewhere in your app. HyperTrack accesses location and activity data, so exact set of permissions depends on an Android version. You may use HyperTrack.requestPermissionsIfNecessary()
convenience method to request permissions and make SDK integration simpler.
Identify your devices
HyperTrack uses string device identifiers that could be obtained from the SDK instance
- Java
- Kotlin
Make sure you've saved this device identifier as it is required when calling HyperTrack Devices and Trips APIs.
Tracking your device
Once you integrated the SDK into your app, you can start tracking your device.
In order to test and verify your SDK integration, you can use either PlayGround in the Dashboard or call Devices and Trips APIs from your server.
- You can start tracking your device with the API
- You can create trips with destination
- You can also create trips with geofences
Dashboard
Once your app is running, go to the Dashboard where you can see a list of all your devices and their live location with ongoing activity on the map.
Advanced integration
Add SDK state listener to catch events.
You can subscribe to SDK status changes addTrackingListener
and handle them in the appropriate methods onError(TrackingError)
onTrackingStart()
onTrackingStop()
Customize foreground service notification
HyperTrack tracking runs as a separate foreground service, so when it is running, your users will see a persistent notification. By default, it displays your app icon with text {app name} is running
but you can customize it anytime after initialization by calling:
- Java
- Kotlin
Check out other configurable properties in ServiceNotificationConfig reference
Create geotag
Geotag is an optional method to tag an app event in the app user's location timeline. E.g. marking a task as done, visit notes, proof-of-delivery, accepting an assigned job, etc.
Distances between geotags are automatically computed, posted to you over webhooks, displayed in map views, and reported in insights scores.
You may optionally attach expected location to verify that the actual location where action took place matches your expectation. You can use deviation between actual and expected to detect an incorrect address, or incorrect behavior.
- Java
- Kotlin
Look into documentation for more details.
You are all set
You can now run the app and start using HyperTrack. You can see your devices on the dashboard.
SDK integration examples
To learn more about SDK integration examples, you may visit these resources:
Frequently Asked Questions
Please review this guide to get answer to these questions:
- What API levels (Android versions) are supported?
- Why do I have NoClassDefFoundError?
- Why do I have dependencies conflicts?
- Why do I have persistent notification on my app?
- How do I handle custom ROMs?
- Why does HyperTrack notification show even after my app is terminated?
- How does tracking work in Doze mode?
- What is AAPT: error: attribute android:foregroundServiceType not found?
- Why doesn't setting device metadata and name work in SDK?
- What is device Id that I get from SDK?
- What permissions are required for the SDK to work?
What API levels (Android versions) are supported?
Currently we do support all of the Android versions starting from API 19 (Android 4.4 Kit Kat).
Why do I have NoClassDefFoundError?
I've added SDK and my app started failing with message like:
This takes place because on Android API level 19 and below you cannot have more than 65536 methods in your app (including methods in libraries).
Please check this StackOverflow answer for solutions.
Why do I have dependencies conflicts?
SDK dependencies graph looks like below:
Common problem here is depending on different versions of com.android.support
library components. You can explicitly specify required version by adding it as a dependency in your app's build.gradle
, e.g.:
and explicitly force SDK pick app's dependencies
That will take precedence over SDK version and you'll have one version of support library on your classpath.
Why do I have persistent notification on my app?
HyperTrack SDK by default runs as a foreground service. This is to ensure that the location tracking works reliably even when your app is minimized.
A foreground service is a service that the user is actively aware of and isn't a candidate for the system to kill when it is low on memory.
Android mandates that a foreground service provides a persistent notification in the status bar. This means that the notification cannot be dismissed by the user.
How do I handle custom ROMs?
Smartphones are getting more and more powerful, but the battery capacity is lagging behind. Device manufacturers are always trying to squeeze some battery saving features into the firmware with each new Android release. Manufactures like Xiaomi, Huawei and OnePlus have their own battery savers that kills the services running in the background.
To inform your users and direct them to the right setting page, you may add the following code in your app. This would intent out your user to the right settings page on the device.
You may also try out open source libraries like AutoStarter.
Some manufacturers don't allow to whitelist apps programmatically.
In that case the only way to achieve service reliability is manual setup. E.g. for Oxygen OS (OnePlus) you need to select Lock menu item from app options button in Recent Apps view:
Why does HyperTrack notification show even after my app is terminated?
The HyperTrack service runs as a separate component and it is still running when the app that started it is terminated. That is why you can observe that notification. When you tracking is stopped, the notification goes away.
How does tracking work in Doze mode?
Doze mode requires device to be stationary, so before OS starts imposing power management restrictions, exact device location is obtained. When device starts moving, Android leaves Doze mode and works regularly, so no special handling of Doze mode required with respect to location tracking.
What is AAPT: error: attribute android:foregroundServiceType not found?
If build fails with error like AAPT: error: attribute android:foregroundServiceType not found
that means that you're targeting your app for Android P or earlier. To fix this update your build tools and set the target platform as Android 10 (target SDK level 30). Although there are other workarounds to fix the build still targeting earlier versions, starting from Android 10 Google imposes additional restrictions on services, that access location data while phone screen is turned off, so the drawback will be tracking gaps on devices that run Android 10 or later.
Why doesn't setting device metadata and name work in SDK?
Devices API or in PlayGround take precedence over SDK methods in setting device name and metadata. If you used either Devices API or PlayGround, these SDK methods setDeviceMetadata
and setDeviceName
will not modify device metadata and name.
What is device Id that I get from SDK?
Device ID uniquely identifies SDK installation. Make sure that you stored it on your backend as a part of user profile to be able to identify location data. On Android 8 and later it is always the same for the app + publishable Key pair and persists across the installation. On devices, powered by earlier versions, it could change after reinstall, although there are some cases in which we are able to keep it the same. Anyway, users change devices and can use one login for multiple phones, so you need to handle that logic of updating device ids in backend.
What permissions are required for the SDK to work
SDK requires following permissions:
- Access Network state
- Internet
- Receive boot completion notification.
- Start Foreground Service.
- Access to devices physical activity, which is
android.permission.ACTIVITY_RECOGNITION
since API 29 andcom.google.android.gms.permission.ACTIVITY_RECOGNITION
before. - Access to coarse and fine location.
- Access to background location is required in order to start tracking from platform side on Android 11.
Most of listed above don't considered dangeourous permissinos, so granted automatically on install. They also included in SDK's AndroidManifest.xml
that is mergerd into your app manifest during the build so you no action required from your side. Contrary to that, your app should request permissions from two last paragraphs interactively if it targets API 29 or later and only location permissions, if it targets API 28 or earlier. This could be achieved via sdkInstance.requestPermissionsIfNecessary()
method invocation that presents required permissions request dialog, if neccessary, or does nothing, if they were already granted.
Background location access permission is special since it cannot be requested interactively, so on Android 11 user is navigated to device's Settings menu where he needs to select Always Allow menu item. HyperTrack SDK shows an info snackbar with message like Please, select "Always Allow" option.
with can be customized by overriding ht_background_permission_toast_template
string resource. Menu item name is taken from OS APIs (so it will be in user's locale), so you need to leave a template placeholder instead of it like Please, select "%" option.
.
Since background location permission complicates Google Play Store review process it is recommended to remove it from the manifest if you don't use platfrom based tracking start in your application, using following tag in your AndroidManifest.xml
: