locked
Time based toast notification in background task RRS feed

  • Question

  • Hello, I want to trigger Toast Notifications in background task after every specified time (e.g. 2 minutes, 5 seconds). The time will be specified by the user on runtime. Is it possible to do that? If yes, how to do that?
    Thursday, July 30, 2015 1:58 AM

Answers

  • The run method purely describes about the task that needs to be done in the background, and you will not put the time trigger here. For example if you want to create a toast notification, the run method will be like this :

        public sealed class ExampleBackgroundTask : IBackgroundTask
        {
            public void Run(IBackgroundTaskInstance taskInstance)
            {
                /*                           *
                *   Creating Toast  *
                *                            */
                // Getting Toast Template
                var template = ToastTemplateType.ToastText01;
                var xml = ToastNotificationManager.GetTemplateContent(template);
    
                // Show Toast
                var toast = new ToastNotification(xml);
                var notifier = ToastNotificationManager.CreateToastNotifier();
                notifier.Show(toast);
            }
        }

    That's it. So where do we put the time trigger? For example, you can implement a button click event in your MainPage.xaml.cs that reads a textbox value and pass the value as an argument to the time trigger and then register the background task, the code example :

    private async void Button1_Click(object sender, RoutedEventArgs e) { var input_time = UInt32.Parse(this.textBox1.Text); var taskRegistered = false; var exampleTaskName = "ExampleBackgroundTask";

    // Check if the task has been registered foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == exampleTaskName) { taskRegistered = true; break; } } if (!taskRegistered) { var builder = new BackgroundTaskBuilder(); builder.Name = exampleTaskName; builder.TaskEntryPoint = "BackgroundTask_Test_Project.ExampleBackgroundTask"; /* Put the input time here, you may want to add try-catch block since the input time must not be lower than 15, the second argument will be false if you want to the task to be executed every specified time */ builder.SetTrigger(new TimeTrigger(input_time, false));

    await BackgroundExecutionManager.RequestAccessAsync(); BackgroundTaskRegistration task = builder.Register(); MessageDialog msg = new MessageDialog("Background Task successfully registered!"); await msg.ShowAsync(); } }




    Friday, July 31, 2015 2:17 AM

All replies

  • You may refer to this link :

    Creating timer based background task

    However, as stated in the link, the timer cannot be set to be less than 15 minutes. If you try to set it to be less than 15 minutes, an exception will occur. 

    If you want to specify the time on runtime, just add a listener to the user input then set the time trigger there and register the background task there.

    About the basic of the background task, you may refer to this link : Creating and registering a background task.

    Hope it helps.

    Thursday, July 30, 2015 10:34 AM
  • I already visited the link you referred.

    But the problem is, we are getting the time at runtime. How can we pass those arguments in BackgroundTask entrypoint method (Run method)? As we need that time to run as a background service.

    Thursday, July 30, 2015 6:57 PM
  • The run method purely describes about the task that needs to be done in the background, and you will not put the time trigger here. For example if you want to create a toast notification, the run method will be like this :

        public sealed class ExampleBackgroundTask : IBackgroundTask
        {
            public void Run(IBackgroundTaskInstance taskInstance)
            {
                /*                           *
                *   Creating Toast  *
                *                            */
                // Getting Toast Template
                var template = ToastTemplateType.ToastText01;
                var xml = ToastNotificationManager.GetTemplateContent(template);
    
                // Show Toast
                var toast = new ToastNotification(xml);
                var notifier = ToastNotificationManager.CreateToastNotifier();
                notifier.Show(toast);
            }
        }

    That's it. So where do we put the time trigger? For example, you can implement a button click event in your MainPage.xaml.cs that reads a textbox value and pass the value as an argument to the time trigger and then register the background task, the code example :

    private async void Button1_Click(object sender, RoutedEventArgs e) { var input_time = UInt32.Parse(this.textBox1.Text); var taskRegistered = false; var exampleTaskName = "ExampleBackgroundTask";

    // Check if the task has been registered foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == exampleTaskName) { taskRegistered = true; break; } } if (!taskRegistered) { var builder = new BackgroundTaskBuilder(); builder.Name = exampleTaskName; builder.TaskEntryPoint = "BackgroundTask_Test_Project.ExampleBackgroundTask"; /* Put the input time here, you may want to add try-catch block since the input time must not be lower than 15, the second argument will be false if you want to the task to be executed every specified time */ builder.SetTrigger(new TimeTrigger(input_time, false));

    await BackgroundExecutionManager.RequestAccessAsync(); BackgroundTaskRegistration task = builder.Register(); MessageDialog msg = new MessageDialog("Background Task successfully registered!"); await msg.ShowAsync(); } }




    Friday, July 31, 2015 2:17 AM
  • Are you trying to get a toast to appear at a specific user-determined time? As in, user sets a reminder for Dentist at 8:00 AM, and you want a toast notification to appear at 8:00 AM?

    If so, you want to use a ScheduledToastNotification. You can set the time that you'd like the toast to appear. No background task required.

    • Proposed as answer by andrewbares7 Friday, September 4, 2015 12:07 AM
    Friday, September 4, 2015 12:06 AM