public async Task Process(Intent intent) { var e = GeofencingEvent.FromIntent(intent); if (e == null) { return; } if (e.HasError) { Log.Write( LocationLogCategory.Geofence, "Event Error", ("ErrorCode", GeofenceStatusCodes.GetStatusCodeString(e.ErrorCode)) ); } else if (e.TriggeringGeofences != null) { foreach (var triggeringGeofence in e.TriggeringGeofences) { var state = (GeofenceState)e.GeofenceTransition; var region = await this.repository.Get(triggeringGeofence.RequestId); if (region == null) { Log.Write( LocationLogCategory.Geofence, "Not Found", ("RequestId", triggeringGeofence.RequestId) ); } else { try { await this.geofenceDelegate.OnStatusChanged(state, region); if (region.SingleUse) { await this.geofenceManager.StopMonitoring(region.Identifier); } } catch (Exception ex) { Log.Write( ex, ("RequestId", triggeringGeofence.RequestId), ("Transition", state.ToString()) ); } } } } }
public async Task Process(Intent intent) { var e = GeofencingEvent.FromIntent(intent); if (e == null) { return; } if (e.HasError) { Log.Write( LocationLogCategory.Geofence, "Event Error", ("ErrorCode", GeofenceStatusCodes.GetStatusCodeString(e.ErrorCode)) ); } else if (e.TriggeringGeofences != null) { foreach (var triggeringGeofence in e.TriggeringGeofences) { var state = (GeofenceState)e.GeofenceTransition; var region = await this.repository.Get(triggeringGeofence.RequestId); if (region == null) { Log.Write( LocationLogCategory.Geofence, "Not Found", ("RequestId", triggeringGeofence.RequestId) ); } else { await this.delegates.RunDelegates( x => x.OnStatusChanged(state, region), ex => Log.Write( ex, ("RequestId", triggeringGeofence.RequestId), ("Transition", state.ToString()) ) ); } } } }
public async void Start() { GeofenceBroadcastReceiver.Process = async e => { if (e.HasError) { var err = GeofenceStatusCodes.GetStatusCodeString(e.ErrorCode); this.logger.LogWarning("Geofence OS error - " + err); } else if (e.TriggeringGeofences != null) { foreach (var triggeringGeofence in e.TriggeringGeofences) { var state = (GeofenceState)e.GeofenceTransition; var region = await this.Repository.Get(triggeringGeofence.RequestId); if (region == null) { this.logger.LogWarning("Geofence reported by OS not found in Shiny Repository - RequestID: " + triggeringGeofence.RequestId); } else { await this.services .RunDelegates <IGeofenceDelegate>( x => x.OnStatusChanged(state, region), ex => this.logger.LogError($"Error in geofence delegate - Region: {region.Identifier} State: {state}") ) .ConfigureAwait(false); } } } }; var regions = await this.Repository.GetAll(); foreach (var region in regions) { await this.Create(region); } }
public override void OnReceive(Context context, Intent intent) { GeofencingEvent geofencingEvent = GeofencingEvent.FromIntent(intent); if (geofencingEvent.HasError) { string error = GeofenceStatusCodes.GetStatusCodeString(geofencingEvent.ErrorCode); Console.WriteLine($"Error Code: {geofencingEvent.ErrorCode}. Error: {error}"); return; } // Get the transition type. int geofenceTransition = geofencingEvent.GeofenceTransition; // Get the geofences that were triggered. A single event can trigger // multiple geofences. IList <IGeofence> triggeringGeofences = geofencingEvent.TriggeringGeofences; NotificationsService notificationsService = new NotificationsService(context); switch (geofenceTransition) { case Geofence.GeofenceTransitionEnter: notificationsService.SendNotification("¡Entraste!", "¡Te hemos pillado cerca!", 10000); break; case Geofence.GeofenceTransitionExit: notificationsService.SendNotification("¡Saliste!", "¡Te hemos pillado saliendo!", 10000); break; case Geofence.GeofenceTransitionDwell: notificationsService.SendNotification("¡Te quedas!", "¿¡No te mueves!?", 10000); break; default: // Log the error. Console.WriteLine("Broadcast not implemented."); break; } }