0
使用下面的代碼添加地理圍欄後,Notification未顯示.even onResult回調正在迴應成功。GeoFence警報不顯示
添加地理圍欄在GoogleApiClientApi onConnected()
回調方法:
public void onConnected(Bundle bundle) {
LocationServices.GeofencingApi.addGeofences(
googleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()
).setResultCallback(this);
}
請求地理柵欄:
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()
回調;
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();
}
}
意向服務觸發通知:
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 {
}
}
我覺得IntentService不inovked。我錯過了什麼?