Handle geofence notifications in the foreground (XAML)

This topic will guide you through the steps of handling Geofence notification in the foreground of your app.

Roadmap: How does this topic relate to others? See:

Introduction

Once your geofences have been created, you will have to add the logic to handle what happens when a geofence event occurs. Depending on the MonitoredStates that you have set up, you may receive an event when:

  • The user has entered a region of interest.
  • The user has left a region of interest.
  • The geofence has expired or been removed. Note that a background app is not activated for a removal event.

You can listen for events directly from your app when it is running or register for a background task so that you receive a background notification when an event occurs. For more info about background task and geofences, see Listen for geofence events in the background, Handle geofence notifications from a background task and Guidelines for geofencing.

Register for geofence state change events

In order for your app to receive a foreground notification of a geofence state change, you must register an event handler. This is typically set up when you create the geofence.

private void Initialize()
{
    // other initialization logic

    GeofenceMonitor.Current.GeofenceStateChanged += OnGeofenceStateChanged;
}

Implement the geofence event handler

The next step is to implement the event handlers. The action taken here will depend on what your app is using the geofence for.

public async void OnGeofenceStateChanged(GeofenceMonitor sender, object e)
{
    var reports = sender.ReadReports();

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        foreach (GeofenceStateChangeReport report in reports)
        {
            GeofenceState state = report.NewState;

            Geofence geofence = report.Geofence;

            if (state == GeofenceState.Removed)
            {
                // remove the geofence from the geofences collection
                GeofenceMonitor.Current.Geofences.Remove(geofence);
            }
            else if (state == GeofenceState.Entered)
            {
                // Your app takes action based on the entered event

                // NOTE: You might want to write your app to take particular
                // action based on whether the app has internet connectivity.

            }
            else if (state == GeofenceState.Exited)
            {
                // Your app takes action based on the exited event

                // NOTE: You might want to write your app to take particular
                // action based on whether the app has internet connectivity.

            }
        }
    });
}

Tasks

Set up a geofence

Listen for geofence events in the background

Handle geofence notifications from a background task

Other resources

Windows 10 geolocation sample

Windows 8.1 geolocation sample

Roadmap for apps using C# and Visual Basic

Roadmap for apps using C++

Designing UX for apps

Guidelines for geofencing

Geoshape

Geofence

Geolocator