简体   繁体   English

在 Awareness Api 中获取 fenceKey null

[英]Getting fenceKey null in Awareness Api

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.嗨,我正在使用必须在用户开始驾驶时通知的应用程序,我使用了 Neura Api,但它需要一个固定的通知,所以我正在尝试使用 Awareness Api。 I need the broadcast in the AndroidManifest.xml because i want to trigger the event even the app is not in background.我需要 AndroidManifest.xml 中的广播,因为即使应用程序不在后台,我也想触发事件。 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. Fence 注册良好,广播被触发,但我无法获得 fenceKey 和 fenceStatus ,我正在尝试使用不同的事件进行测试。

In the AndroidManifest.xml i added permissions, api key and declared the broadcast .在 AndroidManifest.xml 中,我添加了权限、api 密钥并声明了广播

         <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这是我的 FenceReceiver.kt,这是当我遇到耳机围栏事件时的问题,我尝试获取 fenceKey 和 fenceStatus 但我得到了 fenceKey = null 和 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这是我注册 Fences 的地方,在这里检查并接受所有权限,注册工作正常


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 :将挂起的意图标志从FLAG_IMMUTABLE更改为FLAG_MUTABLE

val pendingIntent = PendingIntent.getBroadcast(
    activity.applicationContext, 0,
    intent,
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.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.不幸的是,文档没有显示如何在https://developers.google.com/awareness/android-api/fence-register中创建 mPendingIntent,但对我来说,这成功了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM