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;
    }
}
  •   @sandeepchads @AzureDevOps it's not there anymore; the morning i had the feature had a banner in the top of the page that had a button "try"
  •   @sandeepchads @AzureDevOps I had the feature yesterday morning on my environment and in the evening it was gone again 😭
  •   @markscholman @windowsdev Thanks! Jij ook!
  •   Another year @windowsdev MVP #MVPBuzz https://t.co/blu1TMBO0E
  •   RT @kevintgallo: Check it out https://t.co/F9exR1pQMx
  •   RT @stanzillaz: The new Windows Terminal is now on the Store https://t.co/n8rjsg19dv #WindowsInsiders
  •   @tfsbuck maybe not that clear as i was using my phone. in the release note notification I still see the annoucement… https://t.co/tx8DDm36q8
  •   @hermitdave @JenMsft Notifications In your phone, feature in that app
  •   @hermitdave @JenMsft same for apps, surface book has notifications while surface go hasn't for example
  •   @tfsbuck Rollout sprint 152 complete? in notification I still see thee notice of the build content, while other org… https://t.co/SujHBEYP5v