地理围栏未触发我的反应本机实现

问题描述 投票:0回答:2

不确定这是否与我尝试在react-native模块中使用它有关,它或多或少是相同的 - 除非在ReactContextBaseJavaModule中初始化它是一个问题。

一切似乎都很顺利。围栏区域已添加到 API,没有错误,但从未触发。

也许我错过了一些琐碎的事情,但已经阅读了多个教程和文档。

代码:

public class BackgroundFence extends ReactContextBaseJavaModule {
  private GeofencingClient geofencingClient;
  private String TAG = "FENCE:BackgroundFence";

  BackgroundFence(ReactApplicationContext context) {
    super(context);
  }

  @ReactMethod(isBlockingSynchronousMethod = false)
  public void createFence(double latitude, double longitude,int radius) {
    Log.d("BackgroundFence", "Create fence for " + latitude +":"+longitude+" r:"+radius);
    Activity activity = this.getReactApplicationContext().getCurrentActivity();
    Context context = activity.getBaseContext();
    geofencingClient = LocationServices.getGeofencingClient(context);
    Geofence geofence = createGeofence(
      latitude,
      longitude,
      radius,
      Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT
    );
    GeofencingRequest geofencingRequest = new GeofencingRequest.Builder()
      .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
      .addGeofence(geofence)
      .build();

    List<Geofence> fences = geofencingRequest.getGeofences();
    Log.d(TAG,"Fences: "+fences.size());
    for (Geofence fence : fences) {
      Log.d(TAG,"Fence: "+fence.getRequestId());
    }

    PendingIntent pendingIntent = getGeofencePendingIntent();
    if (activity == null || ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      Log.d(TAG,"No activity or permission");
      return;
    }
    geofencingClient.addGeofences(geofencingRequest, pendingIntent)
      .addOnSuccessListener(activity, new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
          Log.d(TAG, "onSuccess: Geofence Added...");
        }
      })
      .addOnFailureListener(activity, new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
          Log.d(TAG, "onFailure: " + e.getMessage());
        }
      });

  }

  private PendingIntent getGeofencePendingIntent() {
    ReactApplicationContext context = this.getReactApplicationContext();
    Intent intent = new Intent(context, GeofenceBroadcastReceiver.class);
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE );
  }

  public Geofence createGeofence(double latitude, double longitude, float radius, int transitionTypes) {
    return new Geofence.Builder()
      .setRequestId(generateRequestId())
      .setCircularRegion(latitude, longitude, radius)
      .setExpirationDuration(Geofence.NEVER_EXPIRE)
      .setTransitionTypes(transitionTypes)
      .build();
  }

  private String generateRequestId() {
    return UUID.randomUUID().toString();
  }

  @Override
  public String getName() {
    return "BackgroundFence";
  }
}

清单:

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

      <receiver
        android:name=".GeofenceBroadcastReceiver"
        android:enabled="true"
        android:exported="true"
        />

接收者:

public class GeofenceBroadcastReceiver extends BroadcastReceiver {
  private String TAG = "FENCE:GeofenceBroadcastReceiver";
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "onReceive!");

    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if(geofencingEvent==null) {
      Log.d(TAG, "onReceive: geofencingEvent is null");
      return;
    }
    if (geofencingEvent.hasError()) {
      Log.d(TAG, "onReceive: Error receiving geofence event...");
      return;
    }

    int transition = geofencingEvent.getGeofenceTransition();

    if (transition == Geofence.GEOFENCE_TRANSITION_ENTER) {
      Log.d(TAG, "onReceive: ENTER");
    } else if (transition == Geofence.GEOFENCE_TRANSITION_EXIT) {
      Log.d(TAG, "onReceive: EXIT");
    }
  }
}
android react-native broadcastreceiver geofencing
2个回答
0
投票

任何遇到类似问题的人。地理围栏事件不会在模拟器中触发。我花了一整天才弄清楚这个问题。


0
投票

在 iOS 设备上遇到问题。在模拟器上运行良好...但间歇性甚至根本不触发

© www.soinside.com 2019 - 2024. All rights reserved.