Share via


Handle geofence notifications from a background task (HTML)

This topic will guide you through the steps of handling Geofence notifications from a background task, in 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.

This topic describes how to handle a geofence notification when a background task has been triggered. It assumes you have set up a background event listener as described in Listen for geofence events in the background. You can also handle events directly from your app when it is running. For more info, see Handle geofence notifications in the foreground and Guidelines for geofencing.

Handling the background notification

The actions you take to notify the user depends on what your app does but you could possibly display a toast notification, play an audio sound, or update a live tile. The code below handles the notification:


function onCompleted() {
    try {
        // Update the UI with the completion status of the background task
        // The Run method of the background task sets the LocalSettings. 
        var settings = Windows.Storage.ApplicationData.current.localSettings;

        // get status
        if (settings.values.hasKey("Status")) {
            WinJS.log && WinJS.log(settings.values["Status"].toString(), "sample", "status");
        }

        // do your apps work here

    } catch (ex) {
        // The background task had an error
        WinJS.log && WinJS.log(ex.toString(), "sample", "error");
    }
}

Roadmaps

Roadmap for apps using JavaScript

Designing UX for apps

Tasks

Set up a geofence

Handle geofence notifications in the foreground

Listen for geofence events in the background

Reference

Geoshape

Geofence

Geolocator

Other resources

Windows 10 geolocation sample

Windows 8.1 geolocation sample

Guidelines for geofencing