How to add Geofencing on windows silver silverlight 8.1

I want to implement Geofencing in my Windows Phone 8 project, I know the new Geofencing API is launched for Windows Phone 8.1, so I am upgrading my project to Windows Phone 8.1. Now my project has become a Silverlight 8.1 Windows Phone. I want to trigger a notification on the background as well as on the field when the user reaches the place of their desire. I have google search but everything I found for Windows Phone 8.1 is not silverlight 8.1. I tried to add this example to my application so that it doesn't throw any errors, my application works fine but geofencing won't start it ... Please can someone help me on this

Here is what I tried in my application, maybe you can help with that as well.

First, I created class A with the following code.

public class GeofenceService
{
    public static IList<Geofence> GetGeofence()
    {
        return GeofenceMonitor.Current.Geofences;
    }

    public static void CreateGeofence(string id, double lat, double lon, double radius)
    {
        if (GeofenceMonitor.Current.Geofences.FirstOrDefault(i => i.Id == id) != null) return;

        var position = new BasicGeoposition();
        position.Latitude = lat;
        position.Longitude = lon;

        var geocircle = new Geocircle(position, radius);

        MonitoredGeofenceStates mask = 0;
        mask |= MonitoredGeofenceStates.Entered;

        var geofence = new Geofence(id, geocircle, mask, false, new TimeSpan(0, 0, 5));
        GeofenceMonitor.Current.Geofences.Add(geofence);
    }

    public static void RemoveGeofence(string id)
    {
        var geofence = GeofenceMonitor.Current.Geofences.SingleOrDefault(g => g.Id == id);

        if (geofence != null)
            GeofenceMonitor.Current.Geofences.Remove(geofence);
    }

}

      

Then the background task class

public sealed class LocationTask : IBackgroundTask
{
    static string TaskName = "GeofenceUniversalAppLocationTask";

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        // Uncomment this to utilize async/await in the task
        //var deferral = taskInstance.GetDeferral();

        // Get the information of the geofence(s) that have been hit
        var reports = GeofenceMonitor.Current.ReadReports();
        var report = reports.FirstOrDefault(r => (r.Geofence.Id == "MicrosoftStudioE") && (r.NewState == GeofenceState.Entered));

        if (report == null) return;

        // Create a toast notification to show a geofence has been hit
        var toastXmlContent = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);

        var txtNodes = toastXmlContent.GetElementsByTagName("text");
        txtNodes[0].AppendChild(toastXmlContent.CreateTextNode("Geofence triggered toast!"));
        txtNodes[1].AppendChild(toastXmlContent.CreateTextNode(report.Geofence.Id));

        var toast = new ToastNotification(toastXmlContent);
        var toastNotifier = ToastNotificationManager.CreateToastNotifier();
        toastNotifier.Show(toast);

        // Uncomment this to utilize async/await in the task
        //deferral.Complete();
    }

    public async static void Register()
    {
        if (!IsTaskRegistered())
        {
            var result = await BackgroundExecutionManager.RequestAccessAsync();
            var builder = new BackgroundTaskBuilder();

            builder.Name = TaskName;
            builder.TaskEntryPoint = typeof(LocationTask).FullName;
            builder.SetTrigger(new LocationTrigger(LocationTriggerType.Geofence));

            // Uncomment this if your task requires an internet connection
            //var condition = new SystemCondition(SystemConditionType.InternetAvailable);
            //builder.AddCondition(condition);

            builder.Register();
        }
    }

    public static void Unregister()
    {
        var entry = BackgroundTaskRegistration.AllTasks.FirstOrDefault(kvp => kvp.Value.Name == TaskName);

        if (entry.Value != null)
            entry.Value.Unregister(true);
    }

    public static bool IsTaskRegistered()
    {
        var taskRegistered = false;
        var entry = BackgroundTaskRegistration.AllTasks.FirstOrDefault(kvp => kvp.Value.Name == TaskName);

        if (entry.Value != null)
            taskRegistered = true;

        return taskRegistered;
    }
}

      

and finally I call the Geofence function to add geolocation and background task registration and also I declare correctly on Packege.appxmanifest. but still doesn't work :( please your help would be appreciated.

+3


source to share





All Articles