我在FirebaseMessagingService
收到地理围栏时尝试添加地理围栏。我成功地能够添加地理围栏但由于某种原因,它不会发射。我试图调试,看看问题是什么。但我根本无法弄清楚问题是什么。如果有人能指出错误。我非常感谢。
这是我的清单
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<service
android:name=".GeofenceTrasitionService"
android:exported="true"/>
<service
android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".FirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
现在当我收到添加地理围栏的特定消息时,我将其添加到onReceive().
public class FirebaseMessagingService extends FirebaseMessagingService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.i(TAG, "message_received");
Map<String, String> data = remoteMessage.getData();
String messageType = data.get("messageType");
switch (messageType) {
case "NewLocationAdded": {
if (checkPermission()) {
Completable.create(new CompletableOnSubscribe() {
@Override
public void subscribe(@io.reactivex.annotations.NonNull CompletableEmitter e) throws Exception {
GEOFENCE_REQ_CODE_STRING = data.get("locationid");
locationName = data.get("locationname");
interval = Long.parseLong(data.get("interval"));
fastestInterval = Long.parseLong(data.get("fastestinterval"));
locationRadius = Long.parseLong(data.get("locationradius"));
SharedPreferences sharedPreferences = getSharedPreferences("UserDetails", MODE_PRIVATE);
DatabaseReference databaseReference = com.google.firebase.database.FirebaseDatabase
.getInstance()
.getReference("Locations" + "/" + sharedPreferences.getString("UserID", "") + "/" + GEOFENCE_REQ_CODE_STRING);
GeoFire geoFire = new GeoFire(databaseReference);
geoFire.getLocation("Location", new LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
x = location.latitude;
y = location.longitude;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
latLng = new LatLng(x, y);
createGoogleApi();
mGoogleApiClient.connect();
}
}).observeOn(Schedulers.io()).subscribeOn(Schedulers.computation())
.subscribe(new DisposableCompletableObserver() {
@Override
public void onComplete() {
}
@Override
public void onError(@io.reactivex.annotations.NonNull Throwable e) {
Log.i(TAG, e.toString());
}
});
} else {
//Show Notitfication To Give Permission
}
break;
}
}
这可以按预期工作。当我的mGoogleApiClient
已关联时,我添加了geofence
。
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.i(TAG, "Connected to GoogleApiClient");
startLocationUpdates();
Geofence geofence = createGeofence(latLng, locationRadius, GEOFENCE_REQ_CODE_STRING);
GeofencingRequest geofenceRequest = createGeofenceRequest(geofence);
addGeofence(geofenceRequest);
}
为了清楚起见,我将包括上述功能。他们所做的是相当明星的前进。
// Create GoogleApiClient instance
private void createGoogleApi() {
Log.i(TAG, "createGoogleApi()");
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
// Start location Updates
private void startLocationUpdates() {
Log.i(TAG, "startLocationUpdates()");
locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(300)
.setFastestInterval(200);
if (checkPermission())
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}
// Create a Geofence
private Geofence createGeofence(LatLng latLng, float radius, String locationid) {
Log.i(TAG, "createGeofence");
return new Geofence.Builder()
.setRequestId(locationid)
.setCircularRegion(latLng.latitude, latLng.longitude, radius)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER
| Geofence.GEOFENCE_TRANSITION_EXIT | Geofence.GEOFENCE_TRANSITION_DWELL)
.setLoiteringDelay(30)
.build();
}
// Create a Geofence Request
private GeofencingRequest createGeofenceRequest(Geofence geofence) {
Log.i(TAG, "createGeofenceRequest");
return new GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.addGeofence(geofence)
.build();
}
// Add the created GeofenceRequest to the device's monitoring list
private void addGeofence(GeofencingRequest request) {
Log.i(TAG, "addGeofence");
if (checkPermission()) {
LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, request, createGeofencePendingIntent())
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
Log.i(TAG, "onResult: " + status);
if (status.isSuccess()) {
saveGeofence();
// drawGeofence();
} else {
Log.i(TAG, "I failed in addGeofence()");
}
}
});
}
}
private PendingIntent createGeofencePendingIntent() {
Log.i(TAG, "createGeofencePendingIntent");
if (geoFencePendingIntent != null)
return geoFencePendingIntent;
Intent intent = new Intent(this, GeofenceTrasitionService.class);
return PendingIntent.getService(this, GEOFENCE_REQ_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private void saveGeofence() {
Log.i(TAG, "saveGeofence()");
ArrayList<String> stringArrayList = new ArrayList<String>();
SharedPreferences sharedPreferences = getSharedPreferences("Locations", MODE_PRIVATE);
if (sharedPreferences.contains("GeofenceReqIDs")) {
stringArrayList.addAll(sharedPreferences.getStringSet("GeofenceReqIDs", null));
}
SharedPreferences.Editor editor = sharedPreferences.edit();
stringArrayList.add(GEOFENCE_REQ_CODE_STRING);
Set<String> stringSet = new HashSet<String>();
stringSet.addAll(stringArrayList);
editor.putStringSet("GeofenceReqIDs", stringSet);
editor.apply();
}
我知道我已经分享了很多代码,但为了清楚我的问题,我这样做了。我还发布了日志。 Lemme也分享了它们。
checkPermission()
createGoogleApi()
Connected to GoogleApiClient
startLocationUpdates()
checkPermission()
createGeofence
createGeofenceRequest
addGeofence
checkPermission()
createGeofencePendingIntent
onLocationChanged [Location[fused 25.314668,55.495936 acc=21 et=+5d3h47m13s84ms]]
onResult: Status{statusCode=SUCCESS, resolution=null}
saveGeofence()
如果有人能够指出问题所在。我真的很感激它:)