Geofence in the background Windows Phone 8.1 (WinRT)

Problem

I try to call BackgroundTask when a Geofence (Enter / Exit) event occurs in WP8.1 (WinRT). I wrote an example application to try to get it to work, but it seems it can't do it.

So far, these have been the steps I took to try to get Geofences to work in the background:

  • Check accommodation options
  • Create + Register geo-photography
  • Create + register a BackgroundTask that listens for LocationTrigger(LocationTriggerType.Geofence);
  • In my background task, a simple popup notification is triggered

What I did to troubleshoot

I included in my app.manifest:

  • Toast Capable => Yes
  • Features: location, Internet (client & amp; Server)
  • Classifieds: BackgroundTasks (Location). EntryPoint = BackgroundTask.GeofenceBackgroundTask

My background task is in a separate project called BackgroundTask . This is a component of WindowsRT and contains one GeofenceBackgroundTask class.

Project layout

Project example

The project code can be found on this [link] ( https://github.com/kiangtengl/GeofenceSample ):

How to test

  • Run the code in the emulator

  • Set the location for: Latitude = 01.3369, Longitude = 103.7364

enter image description here

  1. Click Register Geofence + BackgroundTasks

  2. Exit the application (press the home button)

  3. Change your current location anywhere within 100 meters of the location you previously set. A notification should appear.

Project Code:

Check location options

  public static async Task GetLocationCapabilities() { try { var geolocator = new Geolocator(); await geolocator.GetGeopositionAsync(); var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync(); Debug.WriteLine("background access status" + backgroundAccessStatus); } catch (UnauthorizedAccessException e) { Debug.WriteLine(e); } catch (TaskCanceledException e) { Debug.WriteLine(e); } } 

Create Geofence

  public static void CreateGeofence(BasicGeoposition position, double radius, string id = "default") { // The Geofence is a circular area centered at (latitude, longitude) point, with the // radius in meter. var geocircle = new Geocircle(position, radius); // Sets the events that we want to handle: in this case, the entrace and the exit // from an area of intereset. var mask = MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited; // Specifies for how much time the user must have entered/exited the area before // receiving the notification. var dwellTime = TimeSpan.FromSeconds(1); // Creates the Geofence and adds it to the GeofenceMonitor. var geofence = new Geofence(id, geocircle, mask, false, dwellTime); try { GeofenceMonitor.Current.Geofences.Add(geofence); } catch (Exception e) { Debug.WriteLine(e); // geofence already added to system } } 

Register background task

  public static async Task RegisterBackgroundTask() { try { // Create a new background task builder var geofenceTaskBuilder = new BackgroundTaskBuilder() { Name = GeofenceBackgroundTaskName, TaskEntryPoint = "BackgroundTask.GeofenceBackgroundTask" }; // Create a new location trigger var trigger = new LocationTrigger(LocationTriggerType.Geofence); // Associate the location trigger with the background task builder geofenceTaskBuilder.SetTrigger(trigger); var geofenceTask = geofenceTaskBuilder.Register(); // Associate an event handler with the new background task geofenceTask.Completed += (sender, e) => { try { e.CheckResult(); } catch(Exception error) { Debug.WriteLine(error); } }; } catch(Exception e) { // Background task probably exists Debug.WriteLine(e); } } 

BackgroundTask code to run the toast

 namespace BackgroundTask { public sealed class GeofenceBackgroundTask : IBackgroundTask { public void Run(IBackgroundTaskInstance taskInstance) { var toastTemplate = ToastTemplateType.ToastText02; var toastXML = ToastNotificationManager.GetTemplateContent(toastTemplate); var textElements = toastXML.GetElementsByTagName("text"); textElements[0].AppendChild(toastXML.CreateTextNode("You have left!")); var toast = new ToastNotification(toastXML); ToastNotificationManager.CreateToastNotifier().Show(toast); } } } 
+6
source share
2 answers

I found that the above code example as well as the code above work. The problem I ran into was that Windows Phone 8.1 does not automatically trigger the Geofence event. You must wait a certain time <5 minutes before starting BackgroundTask.

This also applies to Geofencing in the foreground.

+3
source

I am busy with the same stuff, and I also noticed this behavior, but for me it is 2 minutes. Unfortunately, it always triggers after 2 minutes, even if there were no changes in location and is still inside the fence.

0
source

All Articles