4
votes

I need to trigger a background task from interactive toast notification by pressing action button. i don't know what i'm doing wrong. i'm able to register a task and see it in visual studio. even i can debug it (debuger jumps to MyToastNotificationBackgroundTask.Run function but argument IBackgroundTaskInstance taskInstance is empty object), clicking on a button never runs the task or at least debuger dosn't show it.

I'm registering a background task like this

var builder = new BackgroundTaskBuilder();
builder.Name = "MyToastNotificationBackgroundTask";
builder.TaskEntryPoint = "Tasks.MyToastNotificationBackgroundTask";
builder.SetTrigger(new ToastNotificationActionTrigger());
BackgroundTaskRegistration task = builder.Register();

showing notification

ToastNotifier toastNotifier = ToastNotificationManager.CreateToastNotifier();
ScheduledToastNotification myToastNotificaton = new ScheduledToastNotification(this.myToastXml, DateTime.Now.AddMinutes(1), TimeSpan.FromMinutes(60), 2);
myToastNotificaton .Id = "toast_54ahk36s";
toastNotifier.AddToSchedule(myToastNotificaton);

in app manifest

<Extensions>
    <Extension Category="windows.backgroundTasks" EntryPoint="Tasks.MyToastNotificationBackgroundTask">
      <BackgroundTasks>
        <Task Type="systemEvent" />
      </BackgroundTasks>
    </Extension>
</Extensions>

in toast template xml action buttons are

<actions>
    <input id="message" type="text" placeholderContent="200" />
    <action activationType="background" content="Count" arguments="count" />
</actions>

background task itsetf

namespace Tasks
{
    public sealed class MyToastNotificationBackgroundTask : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            var details = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail;
            ...
        }
    }
}

i can't understand how specifying activationype="background" on a notification templates action button is relating to MyToastNotificationBackgroundTask task? i can't find relevant info on that.

somebody please share your knowledge. maybe you have a working example or smf. any help will be appreciated. thanks in advance.

1

1 Answers

1
votes

I can't understand how specifying activationype="background" on a notification templates action button is relating to MyToastNotificationBackgroundTask task? i can't find relevant info on that.

The trigger type of the background task (ToastNotificationActionTrigger) is what connects the Toast action to the background task. When user taps the action, app looks for a background task with ToastNotificationActionTrigger trigger to and runs if it finds one.

I used your code but couldn't reproduce the problem, trigger works fine. My guess is that you have already registered a task with that name but without the correct trigger type (try unregister first) -or- you have a spelling issue in the toast xml (activationType field).