我需要有关新的Api GeofencingClient的帮助。我正在尝试开发基于位置的提醒,但是我在通知部分上遇到了一些问题。当应用程序被杀死或在后台运行时,我找不到任何通知用户的方法。
这些是我的代码的一部分。
地理围栏代码的一部分。
private void addReminder(ReminderDetails reminder){
Geofence geofence=buildGeofence(reminder);
mGeofenceList.add(geofence);
Log.d(TAG, "addReminder: inside");
if(geofence!=null&& ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
mGeofencingClient.addGeofences(buildGeofencingReq(geofence),getGeofencePendingIntent()).addOnCompleteListener(this);
}
}
private GeofencingRequest buildGeofencingReq(Geofence geofence) {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
Log.d(TAG, "GeofencingRequest: inside");
// The INITIAL_TRIGGER_ENTER flag indicates that geofencing service should trigger a
// GEOFENCE_TRANSITION_ENTER notification when the geofence is added and if the device
// is already inside that geofence.
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
// Add the geofences to be monitored by geofencing service.
builder.addGeofences(mGeofenceList);
// Return a GeofencingRequest.
return builder.build();
}
private Geofence buildGeofence(ReminderDetails reminder) {
double lat =reminder.latLng.latitude;
double longitude=reminder.latLng.longitude;
double radius=reminder.radius;
Log.d(TAG, "buildGeofence: inside");
return new Geofence.Builder()
.setRequestId(reminder.id)
.setCircularRegion(
lat,
longitude,
(float)radius
)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
}
private PendingIntent getGeofencePendingIntent() {
Log.d(TAG, "PendingIntent : inside");
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceReceiver.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
// addGeofences() and removeGeofences().
mGeofencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.d(TAG, "onComplete: inside");
if (task.isSuccessful()) {
List<ReminderDetails> reminderArrayList = Utils.getAll(this);
reminderArrayList.add(reminder);
Utils.saveAll(this, reminderArrayList);
Log.d(TAG, "onComplete: "+new Gson().toJson(mGeofenceList));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
"ServiceChannel",
"Example Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
setResult(Activity.RESULT_OK);
finish();
} else {
// Get the status code for the error and log it using a user-friendly message.
String errorMessage = GeofenceError.getErrorString(this, task.getException());
Log.w(TAG, errorMessage);
}
}
这是我的TransitionIntentService
public class GeofenceTransitionService extends JobIntentService {
private static final int JOB_ID = 573;
private static final String TAG = "GeofenceTransitionsIS";
private static final String CHANNEL_ID = "channel_01";
private ReminderDetails reminder;
/**
* Convenience method for enqueuing work in to this service.
*/
public static void enqueueWork(Context context, Intent intent) {
enqueueWork(context, GeofenceTransitionService.class, JOB_ID, intent);
Log.d(TAG, "GeofenceTransitionService : enqueueWork");
}
/**
* Handles incoming intents.
* @param intent sent by Location Services. This Intent is provided to Location
* Services (inside a PendingIntent) when addGeofences() is called.
*/
@Override
protected void onHandleWork(Intent intent) {
Log.d(TAG, "GeofenceTransitionService : onHandleWork");
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errorMessage = GeofenceError.getErrorString(this,
geofencingEvent.getErrorCode());
Log.e(TAG, errorMessage);
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
// Get the geofences that were triggered. A single event can trigger multiple geofences.
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
// Get the transition details as a String.
String geofenceTransitionDetails = getGeofenceTransitionDetails(geofenceTransition,
triggeringGeofences);
// Send notification and log the transition details.
sendNotification(geofenceTransitionDetails);
Log.i(TAG, geofenceTransitionDetails);
} else {
// Log the error.
Log.e(TAG, getString(R.string.geofence_transition_invalid_type, geofenceTransition));
}
}
我的接收器
public class GeofenceReceiver extends BroadcastReceiver {
private static final String TAG = "GeofenceReceiver";
/**
* Receives incoming intents.
*
* @param context the application context.
* @param intent sent by Location Services. This Intent is provided to Location
* Services (inside a PendingIntent) when addGeofences() is called.
*/
@Override
public void onReceive(Context context, Intent intent) {
// Enqueues a JobIntentService passing the context and intent as parameters
GeofenceTransitionService.enqueueWork(context, intent);
Log.d(TAG, "GeofenceReceiver : onReceive");
}
}
当应用被杀死或在后台运行时,我没有收到任何带有这些代码的通知。我对实现通知用户的所有想法持开放态度。
有人可以帮助我吗?