Hi i´m working in app that have to notificate when the user starts driving, i used Neura Api but it needs a fixed notification, so i´m trying it with Awareness Api. I need the broadcast in the AndroidManifest.xml because i want to trigger the event even the app is not in background. The Fence is registered fine, the broadcast is triggered but i can´t get the fenceKey and fenceStatus , i´m trying with different events for test.
In the AndroidManifest.xml i added permissions, api key and declared the broadcast.
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<receiver
android:name=".usescase.receivers.FenceReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.safycab.safycab.FENCE_RECEIVER_ACTION" />
</intent-filter>
</receiver>
<meta-data
android:name="com.google.android.awareness.API_KEY"
android:value="@string/awareness_key" />
This is my FenceReceiver.kt, here is the problem when i got the event of headphones fence i try to get the fenceKey and fenceStatus but i got fenceKey = null and fenceStatus = 0
class FenceReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val fenceState = FenceState.extract(intent)
context.showLocalNotification("fence key " + fenceState.fenceKey + " fence state" + fenceState.currentState)
}
}
Here is where i register the Fences, here all permissions are checked and accepted, the register is working good
class FenceApiUtils(var activity: BaseActivity<*, *>) {
var drivingFence = DetectedActivityFence.starting(DetectedActivityFence.IN_VEHICLE)
var walkingFence = DetectedActivityFence.starting(DetectedActivityFence.ON_FOOT)
val headPhoneFence = HeadphoneFence.during(HeadphoneState.PLUGGED_IN)
fun createFences() {
val intent = Intent(activity, FenceReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(
activity.applicationContext, 0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
Awareness.getFenceClient(activity).updateFences(
FenceUpdateRequest.Builder()
.addFence(VEHICLE_FENCE_KEY, drivingFence, pendingIntent)
.addFence(WALKING_FENCE_KEY, walkingFence, pendingIntent)
.addFence(HEADPHONE_FENCE, headPhoneFence, pendingIntent)
.build()
).addOnSuccessListener {
log("Fence was successfully registered.")
}.addOnFailureListener {
log("Fence could not be registered: ${it.message}")
}
}
}
If i do this method i can check that the fence is correctly registered
fun queryFence(key: String) {
Awareness.getFenceClient(requireActivity())
.queryFences(FenceQueryRequest.forFences(listOf(key))).addOnSuccessListener {
val map = it.fenceStateMap
for (fenceKey in map.fenceKeys) {
val fenceState = map.getFenceState(fenceKey)
requireContext().showLocalNotification(
"Fence " + fenceKey + ": "
+ fenceState?.currentState
+ ", was="
+ fenceState?.previousState
)
}
}.addOnFailureListener {
log(it.message)
}
}
And if i do this i got the user activity correctly
Awareness.getSnapshotClient(requireActivity()).detectedActivity.addOnSuccessListener {
val act = it.activityRecognitionResult
val dtc = act.mostProbableActivity
val conf = dtc.confidence
val activityStr = dtc.toString()
requireContext().showLocalNotification("Activity: $activityStr, Confidence: $conf/100")
}.addOnFailureListener {
log(it.message)
log(it.localizedMessage)
}
Change the pending intent flag from FLAG_IMMUTABLE to FLAG_MUTABLE:
Unfortunately the documentation doesn't show how to create the mPendingIntent in https://developers.google.com/awareness/android-api/fence-register, but for me this did the trick.