public async void Run(IBackgroundTaskInstance taskInstance) { _deferral = taskInstance.GetDeferral(); GeovisitTriggerDetails visit = (GeovisitTriggerDetails)taskInstance.TriggerDetails; var client = DigitServiceBuilder.Get(); var reports = visit.ReadReports(); var msg = string.Join("\n", reports.Select(r => $"{r.Timestamp} {r.StateChange} {r.Position.Coordinate.Point}").ToArray()); await client.LogAsync($"Geovisit:\n{msg}"); _deferral.Complete(); }
private void GetVisitReports(GeovisitTriggerDetails triggerDetails) { // First fill the _visitBackgroundEvents with previous existing reports saved in local settings. _visitBackgroundEvents.Clear(); FillReportCollectionWithExistingReports(); // Read reports from the triggerDetails. IReadOnlyList <Geovisit> reports = triggerDetails.ReadReports(); string visitItemEvent = null; int numReports = reports.Count; DateTimeFormatter formatterLongTime = new DateTimeFormatter("{hour.integer}:{minute.integer(2)}:{second.integer(2)}", new[] { "en-US" }, "US", CalendarIdentifiers.Gregorian, ClockIdentifiers.TwentyFourHour); foreach (Geovisit report in reports) { visitItemEvent = formatterLongTime.Format(report.Timestamp); visitItemEvent += " " + report.StateChange.ToString(); // Check if the report has a valid position. if (report.Position != null) { visitItemEvent += " (" + report.Position.Coordinate.Point.Position.Latitude.ToString() + "," + report.Position.Coordinate.Point.Position.Longitude.ToString() + ")"; } // Now add each new visit report to _VisitBackgroundEvents. AddVisitReport(visitItemEvent); } if (numReports != 0) { // Save all the reports back to the local settings. SaveExistingReports(); // NOTE: Other notification mechanisms can be used here, such as Badge and/or Tile updates. DoToast(numReports, visitItemEvent); } }
private void ProcessVisitReports(GeovisitTriggerDetails triggerDetails) { // Load the values saved from last time. JsonArray visitBackgroundEvents = Helpers.LoadSavedJson("BackgroundVisitEventCollection"); // Read reports from the triggerDetails. IReadOnlyList <Geovisit> reports = triggerDetails.ReadReports(); string description = null; foreach (Geovisit report in reports) { description = GenerateVisitDescription(report); // Add to the collection, trimming old events if necessary. if (visitBackgroundEvents.Count == maxEventDescriptors) { visitBackgroundEvents.RemoveAt(maxEventDescriptors - 1); } visitBackgroundEvents.Insert(0, JsonValue.CreateStringValue(description)); } int numReports = reports.Count; if (numReports > 0) { // Save the modified results for next time. _values["BackgroundVisitEventCollection"] = visitBackgroundEvents.Stringify(); if (numReports > 1) { description = $"There are {numReports} new geofence events"; } Helpers.DisplayToast(description); } }