Inheritance: IBackgroundTrigger
        /// <summary>
        /// The register task.
        /// </summary>
        private static async void PushNotifications()
        {
            const string PushNotificationTaskName = "ToastNotifications";

            if (GetRegisteredTask(PushNotificationTaskName) != null)
            {
                return;
            }

            var ns = new NotificationStore();
            ns.Register();

            await BackgroundExecutionManager.RequestAccessAsync();

            await ObtainLockScreenAccess();
            var taskBuilder = new BackgroundTaskBuilder
                                  {
                                      Name = PushNotificationTaskName,
                                      TaskEntryPoint = typeof(PushNotificationTask).FullName
                                  };

            var trigger = new PushNotificationTrigger();
            taskBuilder.SetTrigger(trigger);

            var internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
            taskBuilder.AddCondition(internetCondition);

            try
            {
                taskBuilder.Register();
            }
            catch (Exception exception)
            {
            }
        }
Exemple #2
0
        public static void Register()
        {
             
            foreach (var iter in BackgroundTaskRegistration.AllTasks)
            {
                IBackgroundTaskRegistration mytask = iter.Value;
                if (mytask.Name == "ExampleBackgroundTask")
                {

                    mytask.Unregister(true);
                    break;
                }
            }

           
            var builder = new BackgroundTaskBuilder();
            PushNotificationTrigger trigger = new PushNotificationTrigger();
            builder.SetTrigger(trigger);
            builder.Name = "ExampleBackgroundTask";
            builder.TaskEntryPoint = "Usergrid.Notifications.ExampleBackgroundTask";

            ExampleBackgroundTask.task = builder.Register();
            task.Progress += task_Progress;

            task.Completed += task_Completed;

        }
        private async void registerButton_Click(object sender, RoutedEventArgs e)
        {
            TimeTrigger timetrigger = new TimeTrigger(30, false);
            PushNotificationTrigger notificationTrigger = new PushNotificationTrigger();
            


            var SampleTask = new BackgroundTaskBuilder(); //创建后台任务实例
            SampleTask.Name = "Reminder";  //指定后台任务名称
            SampleTask.TaskEntryPoint = "ReminderComponent.Reminder";//指定后台任务名称
            SampleTask.SetTrigger(timetrigger);//指定后台任务的触发器

            SystemCondition internetCondition = new SystemCondition(SystemConditionType.InternetAvailable);
            SampleTask.AddCondition(internetCondition);

            var access = await BackgroundExecutionManager.RequestAccessAsync();
            if (access == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity)
            {
                BackgroundTaskRegistration task = SampleTask.Register();
                task.Progress += Value_Progress; ;
                task.Completed += Value_Completed; ;
                UpdateUI("", "注册成功");

                registerButton.IsEnabled = false;
                cancelButtton.IsEnabled = true;

                var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
                settings.Values.Remove(task.Name);
            }
            else if (access == BackgroundAccessStatus.Denied)//用户禁用后台任务或后台任务数量已达最大
            {
                await new MessageDialog("您已禁用后台任务或后台任务数量已达最大!").ShowAsync();
            }

        }
        private void RegisterBackgroundTask()
        {
            BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
            PushNotificationTrigger trigger = new PushNotificationTrigger();
            taskBuilder.SetTrigger(trigger);

            // Background tasks must live in separate DLL, and be included in the package manifest
            // Also, make sure that your main application project includes a reference to this DLL
            taskBuilder.TaskEntryPoint = SAMPLE_TASK_ENTRY_POINT;
            taskBuilder.Name = SAMPLE_TASK_NAME;

            try
            {
                BackgroundTaskRegistration task = taskBuilder.Register();
                task.Completed += BackgroundTaskCompleted;
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("Registration error: " + ex.Message, NotifyType.ErrorMessage);
                UnregisterBackgroundTask();
            }
        }