-
Notifications
You must be signed in to change notification settings - Fork 27
Description
Summary
We recently implemented foreground location tracking options as a fallback mechanism to trigger geofences locally when users deny background location permissions. Here's our code implementation:
if (Radar.isTracking()) {
val currentTrackingOption = Radar.getTrackingOptions()
val usingBackgroundTracking = currentTrackingOption.foregroundServiceEnabled.not()
if (usingBackgroundTracking != areBackgroundLocationPermissionsGranted()) {
Radar.stopTracking()
} else {
return
}
}
val trackingOption = RadarTrackingOptions.RESPONSIVE
if (areBackgroundLocationPermissionsGranted()) {
Radar.startTracking(defaultTrackingOption.apply {
foregroundServiceEnabled = false
})
} else if (areLocationPermissionsGranted()) {
with(Radar) {
setForegroundServiceOptions(
RadarTrackingOptions.RadarTrackingOptionsForegroundService(
iconString = resources.getResourceEntryName(R.drawable.ic_push_notif_icon),
iconColor = toHexString(R.color.push_notif_color, context),
text = getString(R.string.text_push_notif)
)
)
startTracking(trackingOption.apply {
foregroundServiceEnabled = true
})
}
}
So far, foreground location tracking is working fine but, we have been getting sporadic reports of this crash:
Fatal Exception: android.app.ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{4e11fc2 u0 com.mudflap.mudflap/io.radar.sdk.RadarForegroundService}
at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2147)
at android.app.ActivityThread.access$2800(ActivityThread.java:310)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2376)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8669)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
I've been investigating possible causes for this crash and found a piece of information in the Android docs that seems to be related to it:
The system allows apps to call Context.startForegroundService() even while the app is in the background. However, the app must call that service's startForeground() method within five seconds after the service is created.
Is there something that can be done to fix or mitigate this crash?
Also, I'd like to know if we're using Radar.stopTracking() correctly in our implementation. We're calling it to stop the tracking when the background location permission status changes, so the tracking mode (foreground or background) is updated based on it. Do we even need to do that or is the SDK capable of doing so out of the box?