首页 > 解决方案 > HMS 地理围栏未触发事件

问题描述

我在尝试使用HMS Geofencing( React Native) 时遇到问题。我正在设置服务,如下所示:

  1. 将地理围栏添加到地理围栏列表
const geofence = {
        latitude: latitude,
        longitude: longitude,
        radius: radius,
        uniqueId: identifier,
        conversions:
          HMSLocation.Geofence.Native.GeofenceConstants.ENTER_GEOFENCE_CONVERSION |
          HMSLocation.Geofence.Native.GeofenceConstants.EXIT_GEOFENCE_CONVERSION,
      }
geofenceList.push(geofence)
  1. 如文档中所述启动地理围栏
async startGeofencingTask(): Promise<void> {
    logger.info('Starting geofencing task')
    try {
      const regions = await this.loadGeofencedPositions()
      // getting geofenceList as shown before from an array of regions
      const geofenceList = this.convertToHMSGeofenceList(regions)
      const conversionType =
        HMSLocation.Geofence.Native.GeofenceRequestConstants.ENTER_INIT_CONVERSION |
        HMSLocation.Geofence.Native.GeofenceRequestConstants.EXIT_INIT_CONVERSION
      const coordinateType = HMSLocation.Geofence.Native.GeofenceRequestConstants.COORDINATE_TYPE_WGS_84

      HMSLocation.Geofence.Native.createGeofenceList(GEOFENCING_TASK_ID, geofenceList, conversionType, coordinateType)
      HMSLocation.Geofence.Events.registerGeofenceHeadlessTask(huaweiGeofencingTask)
      HMSLocation.Geofence.Events.addGeofenceEventListener(huaweiGeofencingTask)
    } catch (error) {
      logger.warn(`Unable to start geofencing task`, error)
    }
 }

使用此设置,我只收到第一个触发器,但从那时起,无论应用程序处于前台还是后台,我都没有收到任何更新。关于为什么我只获得初始更新的任何线索?

编辑: 使用权限设置为文档中的:

<uses-permission 
  android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission 
  android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission 
  android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

标签: react-nativehuawei-mobile-servicesgeofencinghuawei-developers

解决方案


确保您已将位置权限授予应用程序,并且您已在 Android Manifest 中注册了广播接收器。

<receiver
    android:name=".geofence.GeoFenceBroadcastReceiver"
    android:exported="true">

    <intent-filter>
        <action android:name="com.huawei.hmssample.geofence.GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION" />
    </intent-filter>

</receiver>

推荐阅读