我的应用程序是关于任务管理的,我可以为特定任务分配位置。我将该位置的纬度和经度存储在数据库中。现在我想在用户当前位置周围创建运行时地理围栏,并检查存储的latlong是否存在于围栏中。如果存在,那么我想向用户发送通知,告知您您已将任务分配给该位置。
在我的主要活动中,我有create方法来从数据库中获取纬度值。获取当前位置并在其周围生成500m的地理围栏。检查数据库值和当前纬度值是否匹配,如果匹配则发送通知。为发送通知,我创建了后台服务。现在我的服务运行正常并连续发送通知,但是当我从历史记录中删除我的应用程序时,我没有收到Notification.I已尝试使用一个信号推送通知,但没有解决我的问题。 如果不打开我的应用程序,我们可以在后台连续检查这种情况并发送通知吗? 在没有打开应用程序的背景下,我们可以访问sqlite数据库值吗?
private void startGeofence() {
Log.i(TAG, "startGeofence()");
LatLng latLng = new LatLng(latitude, longitude);
Geofence geofence = createGeofence(latLng, GEOFENCE_RADIUS);
GeofencingRequest geofenceRequest = createGeofenceRequest(geofence);
arrayList = dataBaseHelper.getLatlongList();
arrayList.removeAll(Arrays.asList(" ", null));
arrayList.removeAll(Arrays.asList("", null));
Log.d(TAG, "onStart: " + arrayList.toString());
String lat = String.format("%.2f", latitude);
//String lat = String.valueOf(latitude);
for (int j = 0; j < arrayList.size(); j++) {
String first = arrayList.get(j);
String value = String.format("%.5s", first);
if (!value.equals(lat)) {
//addGeofence(geofenceRequest);
} else {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then
overriding
// public void onRequestPermissionsResult(int
requestCode, String[] permissions,
// int[]
grantResults)
// to handle the case where the user grants the
permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
geofencingClient.addGeofences(createGeofenceRequest(geofence),
getGeofencePendingIntent())
.addOnSuccessListener(this, new
OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// Geofences added
Log.d(TAG, "onSuccess: successfully added");
getGeofencePendingIntent();
// ...
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Failed to add geofences
// ...
}
});
}
}
}
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (geoFencePendingIntent != null) {
return geoFencePendingIntent;
}
Intent intent = new Intent(this,
GeofenceTrasitionService.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending
intent
back when
// calling addGeofences() and removeGeofences().
geoFencePendingIntent = PendingIntent.getService(this, 0, intent,
PendingIntent.
FLAG_UPDATE_CURRENT);
return geoFencePendingIntent;
}
if (intent != null) {
GeofencingEvent geofencingEvent =
GeofencingEvent.fromIntent(intent);
// Handling errors
if ( geofencingEvent.hasError() ) {
String errorMsg =
getErrorString(geofencingEvent.getErrorCode() );
Log.e( TAG, errorMsg );
return;
}
// Retrieve GeofenceTrasition
int geoFenceTransition = geofencingEvent.getGeofenceTransition();
// Check if the transition type
if ( geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geoFenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT )
{
// Get the geofence that were triggered
List<Geofence> triggeringGeofences =
geofencingEvent.getTriggeringGeofences();
// Create a detail message with Geofences received
String geofenceTransitionDetails =
getGeofenceTrasitionDetails(geoFenceTransition,
triggeringGeofences );
// Send notification details as a String
try {
OneSignal.postNotification(new JSONObject("{'contents':
{'en':'Test Message'}, 'include_player_ids': ['" + playerId +
"']}"), null);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d(TAG, "onHandleIntent: Send Notification");
//sendNotification( geofenceTransitionDetails );
}
在后台连续检查数据库位置和当前位置(如果当前位置中存在数据库位置),如果我从历史记录中删除了应用程序,围栏也会发送通知。