Search in sources :

Example 1 with GeofencingEvent

use of com.google.android.gms.location.GeofencingEvent in project Android-ReactiveLocation by mcharmas.

the class GeofenceBroadcastReceiver method onReceive.

@Override
public void onReceive(Context context, Intent intent) {
    GeofencingEvent event = GeofencingEvent.fromIntent(intent);
    String transition = mapTransition(event.getGeofenceTransition());
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Geofence action").setContentText(transition).setTicker("Geofence action").build();
    nm.notify(0, notification);
}
Also used : NotificationManager(android.app.NotificationManager) NotificationCompat(android.support.v4.app.NotificationCompat) Notification(android.app.Notification) GeofencingEvent(com.google.android.gms.location.GeofencingEvent)

Example 2 with GeofencingEvent

use of com.google.android.gms.location.GeofencingEvent in project mobile-messaging-sdk-android by infobip.

the class GeoTransitionHelper method resolveTransitionFromIntent.

/**
 * Resolves transition information from geofencing intent
 *
 * @param intent geofencing intent
 * @return transition information
 * @throws RuntimeException if information cannot be resolved
 */
static GeoTransition resolveTransitionFromIntent(Intent intent) throws RuntimeException {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent == null) {
        throw new RuntimeException("Geofencing event is null, cannot process");
    }
    if (geofencingEvent.hasError()) {
        if (geofencingEvent.getErrorCode() == GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE) {
            throw new GeofenceNotAvailableException();
        }
        throw new RuntimeException("ERROR: " + GeofenceStatusCodes.getStatusCodeString(geofencingEvent.getErrorCode()));
    }
    GeoEventType event = supportedTransitionEvents.get(geofencingEvent.getGeofenceTransition());
    if (event == null) {
        throw new RuntimeException("Transition is not supported: " + geofencingEvent.getGeofenceTransition());
    }
    Set<String> triggeringRequestIds = new ArraySet<>();
    for (Geofence geofence : geofencingEvent.getTriggeringGeofences()) {
        triggeringRequestIds.add(geofence.getRequestId());
    }
    Location location = geofencingEvent.getTriggeringLocation();
    return new GeoTransition(event, triggeringRequestIds, new GeoLatLng(location.getLatitude(), location.getLongitude()));
}
Also used : ArraySet(android.support.v4.util.ArraySet) GeoLatLng(org.infobip.mobile.messaging.geo.GeoLatLng) GeoEventType(org.infobip.mobile.messaging.geo.GeoEventType) GeofencingEvent(com.google.android.gms.location.GeofencingEvent) Geofence(com.google.android.gms.location.Geofence) Location(android.location.Location)

Example 3 with GeofencingEvent

use of com.google.android.gms.location.GeofencingEvent in project Remindy by abicelis.

the class GeofenceNotificationIntentService method onHandleIntent.

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = GeofenceErrorMessages.getErrorString(this, geofencingEvent.getErrorCode());
        Log.e(TAG, errorMessage);
        return;
    }
    // Get the transition type.
    int geofenceTransition = geofencingEvent.getGeofenceTransition();
    // Log the transition type
    handleLogTransition(geofenceTransition);
    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT || // || geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER   //Skipping this transition because of alert spam issues
    geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL) {
        for (Geofence geofence : geofencingEvent.getTriggeringGeofences()) {
            List<Task> tasks = new ArrayList<>();
            try {
                tasks = new RemindyDAO(this).getLocationBasedTasksAssociatedWithPlace(Integer.valueOf(geofence.getRequestId()), geofenceTransition);
            } catch (CouldNotGetDataException e) {
                Log.e("TAG", "Could not get data, geofence notification service.", e);
            }
            if (tasks.size() > 0) {
                String notificationTitle = getGeofenceNotificationTitle(geofenceTransition, geofence);
                String notificationText = getGeofenceNotificationText(tasks);
                // Send notification and log the transition details.
                NotificationUtil.displayLocationBasedNotification(this, Integer.valueOf(geofence.getRequestId()), notificationTitle, notificationText, tasks);
                Log.i(TAG, notificationTitle + " " + notificationText);
            }
        }
    }
}
Also used : Task(ve.com.abicelis.remindy.model.Task) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) ArrayList(java.util.ArrayList) RemindyDAO(ve.com.abicelis.remindy.database.RemindyDAO) GeofencingEvent(com.google.android.gms.location.GeofencingEvent) Geofence(com.google.android.gms.location.Geofence)

Example 4 with GeofencingEvent

use of com.google.android.gms.location.GeofencingEvent in project android-play-location by googlesamples.

the class GeofenceTransitionsJobIntentService method onHandleWork.

/**
 * 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) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = GeofenceErrorMessages.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));
    }
}
Also used : GeofencingEvent(com.google.android.gms.location.GeofencingEvent) Geofence(com.google.android.gms.location.Geofence)

Example 5 with GeofencingEvent

use of com.google.android.gms.location.GeofencingEvent in project CodenameOne by codenameone.

the class GeofenceHandler method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    String className = intent.getStringExtra("geofenceClass");
    String id = intent.getStringExtra("geofenceID");
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    for (com.google.android.gms.location.Geofence gf : geofencingEvent.getTriggeringGeofences()) {
        try {
            id = gf.getRequestId();
            GeofenceListener l = (GeofenceListener) Class.forName(className).newInstance();
            if (geofencingEvent.getGeofenceTransition() == com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_ENTER) {
                l.onEntered(id);
            } else if (geofencingEvent.getGeofenceTransition() == com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_EXIT) {
                l.onExit(id);
            }
        } catch (Exception e) {
            Log.e("Codename One", "geofence error", e);
        }
    }
}
Also used : GeofencingEvent(com.google.android.gms.location.GeofencingEvent)

Aggregations

GeofencingEvent (com.google.android.gms.location.GeofencingEvent)7 Geofence (com.google.android.gms.location.Geofence)3 Notification (android.app.Notification)2 NotificationManager (android.app.NotificationManager)2 NotificationCompat (android.support.v4.app.NotificationCompat)2 Location (android.location.Location)1 ArraySet (android.support.v4.util.ArraySet)1 ArrayList (java.util.ArrayList)1 GeoEventType (org.infobip.mobile.messaging.geo.GeoEventType)1 GeoLatLng (org.infobip.mobile.messaging.geo.GeoLatLng)1 RemindyDAO (ve.com.abicelis.remindy.database.RemindyDAO)1 CouldNotGetDataException (ve.com.abicelis.remindy.exception.CouldNotGetDataException)1 Task (ve.com.abicelis.remindy.model.Task)1