public void Run(IBackgroundTaskInstance taskInstance)
        {
            Debug.WriteLine("Run of {0}", typeof(GeofencingBackgroundTask).FullName);

            IoCHelper.Init();

            var alarmsManager = IoC.Get <IAlarmsManager>();

            var reports = GeofenceMonitor.Current.ReadReports()
                          .Where(report => report.NewState == GeofenceState.Entered).Select(report => report.Geofence.Id);
            var triggeredAlarm = alarmsManager.Alarms.FirstOrDefault(alarm => reports.Contains(alarm.GeofenceId));

            if (triggeredAlarm == null)
            {
                Debug.WriteLine("{0} : no such geofence id", typeof(GeofencingBackgroundTask).FullName);
                return;
            }

            if (triggeredAlarm.Enabled)
            {
                Debug.WriteLine("Alarm is active");

                Debug.WriteLine("{0} : showing toast", typeof(GeofencingBackgroundTask).FullName);

                var toastContent = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
                var textNodes    = toastContent.GetElementsByTagName("text");
                textNodes[0].AppendChild(toastContent.CreateTextNode("You are pretty close to "));
                textNodes[1].AppendChild(toastContent.CreateTextNode(triggeredAlarm.Title));

                ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastContent));
            }
            else
            {
                Debug.WriteLine("Alarm is not active");
            }
        }
Ejemplo n.º 2
0
        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
#if DEBUG
            if (Debugger.IsAttached)
            {
                DebugSettings.EnableFrameRateCounter = true;
            }
#endif

            var rootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();

                // TODO: change this value to a cache size that is appropriate for your application
                rootFrame.CacheSize = 1;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    // TODO: Load state from previously suspended application
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            if (rootFrame.Content == null)
            {
                // Removes the turnstile navigation for startup.
                if (rootFrame.ContentTransitions != null)
                {
                    transitions = new TransitionCollection();
                    foreach (var c in rootFrame.ContentTransitions)
                    {
                        transitions.Add(c);
                    }
                }

                rootFrame.ContentTransitions = null;
                rootFrame.Navigated         += RootFrame_FirstNavigated;

                IoCHelper.Init();

                GeofencingBackgroundTask.Register();

                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                var alarmsManager = IoC.Get <IAlarmsManager>();

                //  if (!rootFrame.Navigate(typeof (MapPage), e.Arguments))
                if (!rootFrame.Navigate(typeof(ActiveAlarmPage),
                                        new AlarmItemViewModel(alarmsManager)
                {
                    Alarm = alarmsManager.Alarms.FirstOrDefault()
                }))
                {
                    throw new Exception("Failed to create initial page");
                }
            }

            // Ensure the current window is active
            Window.Current.Activate();
        }