New BackgroundTask Triggers in Windows 10

In the Windows 10 developer preview there are some new background triggers that can be very useful for number of scenario's. I will make a couple of blogposts of which triggers I found.

Let start with the first: ApplicationTrigger. It's a common question I see at MSDN. How can the app itself trigger the background task. the awnser, it can't. Windows 8 apps cant trigger the background task on demand. With Windows 10 this is changing. We are now able to trigger the backgroundtask from the foreground. How it works?

First create a background task for test purpose: - create a Windows Runtime Component as you did in Windows 8 app - create a new class and make a background task implementation. I have this

public sealed class MyBackgroundTask : IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {

    }
}

to see it works i set a breakpoint in the run method. You can also write some code ofcourse. Make also a reference from your app the the winrt component.

Second step is to register your BackgroundTask in the manifest. Since the designer seems to be broken you need to add this xml yourself:

<Extensions>
    <Extension Category="windows.backgroundTasks" EntryPoint="RTLibrary.MyBackgroundTask">
      <BackgroundTasks>
        <Task Type="general" />
      </BackgroundTasks>
    </Extension>
  </Extensions>

It have to be a child of the Application element that is already in the xml.

Third step is to make the trigger. You will need to keep a reference to this trigger so you can invoke it when you want run the task.

private readonly ApplicationTrigger _trigger;

public MainPage()
{
    this.InitializeComponent();

    _trigger = new ApplicationTrigger();
}

After created the trigger you need to register the background task. this is something you should be familiar with.

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            foreach (var t in BackgroundTaskRegistration.AllTasks)
                t.Value.Unregister(true);

            BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
            builder.TaskEntryPoint = "RTLibrary.MyBackgroundTask";
            builder.Name = "AppTrigger";
            builder.SetTrigger(_trigger);
            var registration = builder.Register();
        }

Now for test purpose add a button to your UI and implement the click handler. When the button is clicked we going to trigger the background task. To do this you only need one more line of code. Just call the RequestAysnc method of the trigger:

private async void Button_Click(object sender, RoutedEventArgs e)
        {
            await _trigger.RequestAsync();
        }

Another new trigger is the ToastNotificationHistoryChangedTrigger. When you use this trigger your backgroundtask gets invoked when the user dismisses the toast notification from the notification center. This can be useful to update the Tile for example.

To use this, we keep the code from above. Just going to add a few things. First we change the trigger of the background task registration:

foreach (var t in BackgroundTaskRegistration.AllTasks)
    t.Value.Unregister(true);

BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
builder.TaskEntryPoint = "RTLibrary.MyBackgroundTask";
builder.Name = "AppTrigger";
builder.SetTrigger(new ToastNotificationHistoryChangedTrigger());
var registration = builder.Register();

As you see only changed the Trigger. Below this we add some test code to add a notification to the notification center:

var toastUpdates = ToastNotificationManager.CreateToastNotifier();
var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);

template.GetElementsByTagName("text")[0].InnerText = "Test Notification";

toastUpdates.Show(new ToastNotification(template));

Now we need to add something to the manifest. TO the uap:Application element add this attribute: ToastCapable="true"

Now run the application, make sure you have a breakpoint in the background task. You see for a moment the toast notification. Now open the notification center and swipe the notification away. A few seconds after your breakpoint in your notification gets hit!

Now there is some more information. The triggerdetails contains more information about what kind of change the user maked:

var details = taskInstance.TriggerDetails as ToastNotificationHistoryChangedTriggerDetail;
if (details != null)
{
    switch(details.ChangeType)
    {
        case ToastHistoryChangedType.Added:
            break;
        case ToastHistoryChangedType.Cleared:
            break;
        case ToastHistoryChangedType.Expired:
            break;
        case ToastHistoryChangedType.Removed:
            break;
    }
}
  •   @TwitterSupport great move!
  •   Gave my #ActivityFeed extension for @VSTS some valentine's love by giving it a fresh new look. Get it here… https://t.co/RiTSWr4mQL
  •   @DrGigabit love this too: https://t.co/G3YsSJzafK There is a mismatch between what science knows and what business does
  •   @aaronbjork anything in pipeline to work more with target dates. or at least give something that can be disuccesed witih stakeholders?
  •   @aaronbjork we are using the kanban boards and never the sprints; anymore; this makes delivery plans kind of useless. (1/2)
  •   3 of the @vsts extensions i was involved in now on the homepage of the marketplace https://t.co/mM9h0F8ZVu
  •   @JenMsft w00t mouse delays fixed?
  •   @agent766 drop me an email and will help you dave@familie-smits.com
  •   @willsmythe and for widgets you can include platform styling. can hubs do that ? cant find method for it neither (get same headers etc)
  •   @willsmythe for my Project Teams extension want to make it possible to favorite teams. not really find the right control in that library