After adding a geofence using below code, Notification is not showing.Even onResult callback is returing the success.
Adding geofence in GoogleApiClientApi onConnected()
callback method:
public void onConnected(Bundle bundle) {
LocationServices.GeofencingApi.addGeofences(
googleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()
).setResultCallback(this);
}
Requesting Geofence:
private GeofencingRequest getGeofencingRequest() {
geoFenceList.add(new Geofence.Builder()
.setRequestId("myFence")
.setCircularRegion(68.441630, 77.310587, 2.0f)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build());
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(geoFenceList);
return builder.build();
}
onResult()
callback;
public void onResult(Status status) {
if (status.isSuccess()){
Toast.makeText(this,"Working",Toast.LENGTH_SHORT).show();//This Toast is showing/
}else{
Toast.makeText(this,"Not Working",Toast.LENGTH_SHORT).show();
}
}
Intent Service which triggers the Notification:
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
return;
}
int geofenceTransition = geofencingEvent.getGeofenceTransition();
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Your Geo")
.setContentText("This is My geo")
.setContentIntent(pendingNotificationIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.build();
notificationManager.notify(0, notification);
} else {
}
}
I think the IntentService is not inovked. Did I miss something?
here is my working code.. i hope its help u..
and make sure that you add this line in manifest file:
here is the link that i implemented in my project geofence alert