Esempio n. 1
0
        private async void RegToastBackgroundTask()
        {
            // 判断一下是否允许访问后台任务
            var status = await BackgroundExecutionManager.RequestAccessAsync();

            if (status == BackgroundAccessStatus.Denied || status == BackgroundAccessStatus.Unspecified)
            {
                return;
            }

            Type type = typeof(BackgroundTasks.ToastBackgroundTask);
            var  task = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault(t => t.Name == type.Name);

            if (task == null)
            {
                // 注册后台任务
                var tBuilder = new BackgroundTaskBuilder();
                tBuilder.Name           = type.Name;
                tBuilder.TaskEntryPoint = type.FullName;
                // 声明触发器
                var trigger = new ToastNotificationActionTrigger();
                tBuilder.SetTrigger(trigger);
                task = tBuilder.Register();
            }
        }
Esempio n. 2
0
        private async void btnShowActionToast_Tapped(object sender, TappedRoutedEventArgs e)
        {
            var trigger = new ToastNotificationActionTrigger();
            var reg = await RegisterBackgroundTask(typeof(AdaptiveToastTrigger.ToastTriggerTask).FullName,
                                         trigger.GetType().Name, trigger, null);

            await ShowAdaptiveToast("ms-appx:///assets/AdaptiveActionToast.xml");
        }
        /// <summary>
        /// Registers a particular bg tasks specified by <paramref name="backgroundTask"/>
        /// </summary>
        /// <param name="backgroundTask">The background task to register</param>
        /// <param name="restart">Indicates if the registration of the bg task is mandatory(True by default)</param>
        public static void RegisterBackgroundTask(BackgroundTaskType backgroundTask, int bgTaskInterval = 0, bool restart = true)
        {
            string             bgTaskName;
            Type               bgTaskType;
            IBackgroundTrigger trigger;
            var conditions = new List <IBackgroundCondition>();

            switch (backgroundTask)
            {
            case BackgroundTaskType.ANY:
                throw new ArgumentException("Is not allowed to register all bg tasks at the same time");

            case BackgroundTaskType.SYNC:
                bgTaskName = nameof(SyncBackgroundTask);
                bgTaskType = typeof(SyncBackgroundTask);
                trigger    = new TimeTrigger((uint)bgTaskInterval, false);
                conditions.Add(new SystemCondition(SystemConditionType.FreeNetworkAvailable));
                conditions.Add(new SystemCondition(SystemConditionType.InternetAvailable));
                break;

            case BackgroundTaskType.MARK_AS_COMPLETED:
                bgTaskName = nameof(MarkAsCompletedBackgroundTask);
                bgTaskType = typeof(MarkAsCompletedBackgroundTask);
                trigger    = new ToastNotificationActionTrigger();
                break;

            default:
                throw new ArgumentOutOfRangeException($"Provided bg task {backgroundTask} does not exists");
            }

            bool isBgTaskAlreadyRegistered = BackgroundTaskHelper.IsBackgroundTaskRegistered(bgTaskType);

            if (isBgTaskAlreadyRegistered && !restart)
            {
                return;
            }

            if (isBgTaskAlreadyRegistered)
            {
                UnregisterBackgroundTask(backgroundTask);
            }

            if (bgTaskInterval <= 0 && backgroundTask != BackgroundTaskType.MARK_AS_COMPLETED)
            {
                return;
            }

            BackgroundTaskHelper.Register(
                bgTaskName,
                trigger,
                false,
                true,
                conditions.ToArray());
        }
Esempio n. 4
0
        void ConfigureToastActions()
        {
            try
            {
                var trigger   = new ToastNotificationActionTrigger();
                var condition = new SystemCondition(SystemConditionType.InternetAvailable);

                var builder = new BackgroundTaskBuilder();
                builder.Name = "ToastTask";
                builder.SetTrigger(trigger);
                builder.AddCondition(condition);

                var task = builder.Register();
            }
            catch (Exception ex)
            {
                Diag.DebugPrint("Exception caught while setting up toast actions " + ex.Message);
            }
        }
Esempio n. 5
0
        private async Task InitializeApplication()
        {
            //copy images to appdata
            var local_folder = Windows.Storage.ApplicationData.Current.LocalFolder;
            var install_path = Windows.ApplicationModel.Package.Current.InstalledLocation;
            var media_path   = await install_path.GetFolderAsync("media\\images");

            var images = await media_path.GetFilesAsync();

            foreach (var image in images)
            {
                try
                {
                    await local_folder.GetFileAsync(image.Name);

                    continue;
                }
                catch { }
                await image.CopyAsync(local_folder, image.Name, ReplaceExisting);
            }

            //initialize contacts watcher
            await StartBackgroundTaskAsync(() =>
            {
                var task_name          = "contacts-watcher";
                var previous_task_list = BackgroundTaskRegistration.AllTasks.Values;
                var registered         = previous_task_list.Where(i => i.Name == task_name).FirstOrDefault();
                registered?.Unregister(true);
                //registered = null;
                //if (registered == null)
                //{
                //    BackgroundTaskBuilder task_builder = new BackgroundTaskBuilder();
                //    task_builder.Name = task_name;
                //    task_builder.TaskEntryPoint = typeof(ContactsWatcher).ToString();
                //    ContactStoreNotificationTrigger contact_trigger = new ContactStoreNotificationTrigger();
                //    task_builder.SetTrigger(contact_trigger);
                //    task_builder.Register();
                //}
            });

            //initialize actionable toast server
            await StartBackgroundTaskAsync(() =>
            {
                var task_name          = "contacts-toast-server";
                var previous_task_list = BackgroundTaskRegistration.AllTasks.Values;
                var registered         = previous_task_list.Where(i => i.Name == task_name).FirstOrDefault();
                registered?.Unregister(true);
                registered = null;
                if (registered == null)
                {
                    BackgroundTaskBuilder task_builder = new BackgroundTaskBuilder();
                    task_builder.Name           = task_name;
                    task_builder.TaskEntryPoint = typeof(ActionableToastServer).ToString();
                    ToastNotificationActionTrigger toast_trigger = new ToastNotificationActionTrigger();
                    task_builder.SetTrigger(toast_trigger);
                    task_builder.Register();
                }
            });

            //initialize location
            await InitializeLocation();
        }