I am trying to have a time triggered background task that displays a toast with button. "Are you ready to do xyx?" [Yes] [No]. Clicking Yes will then go to a web page. No will close the toast until the next time period.
In Windows 10 universal apps, I've seen examples where a toast is launched from a universal app with buttons and the buttons are processed in a background task.
I've seen toasts that display a message when the toast is launched from a background task that has no buttons.
What I need to know is how do you process the button if a toast with buttons is launched from a background task.
Is that possible? If so how?
Here is the code used to display the toast in the background task:
namespace BGtask
{
public sealed class BGTimerTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var deferral = taskInstance.GetDeferral();
try
{
SendToast();
}
finally
{
// And finally release the deferral since we're done
deferral.Complete();
}
}
private void SendToast()
{
ToastContent content = new ToastContent()
{
Visual = new ToastVisual()
{
TitleText = new ToastText()
{
Text = "XYZ"
},
BodyTextLine1 = new ToastText()
{
Text = "Are you ready to do xyz?"
}
},
Actions = new ToastActionsCustom()
{
Inputs =
{
new ToastSelectionBox("selection")
{
Items =
{
new ToastSelectionBoxItem("1", "Yes"),
new ToastSelectionBoxItem("2", "Not Now"),
new ToastSelectionBoxItem("3", "Don't Show Again")
},
DefaultSelectionBoxItemId = "1"
}
},
Buttons =
{
new ToastButton("OK", new QueryString()
{
{ "action", "ok" },
}.ToString())
{
ActivationType = ToastActivationType.Background
},
}
}
};
ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(content.GetXml()));
}
}
}
I see, you already set ToastActivationType.Background
. That's the correct setting. You will need to process the button in another background task registered from the same application:
public sealed class NotificationActionBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var details = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail;
if (details != null)
{
string arguments = details.Argument; // button argument
var userInput = details.UserInput;
var selection = userInput["selection"] // dropdown value
// process button
}
}
}
You need to register this task with a ToastNotificationActionTrigger
:
BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();
BackgroundTaskBuilder builder = new BackgroundTaskBuilder()
{
Name = "MyToastTask",
TaskEntryPoint = "BgTask.NotificationActionBackgroundTask"
};
builder.SetTrigger(new ToastNotificationActionTrigger());
BackgroundTaskRegistration registration = builder.Register();
Of course, don't forget to declare the task in the application manifest (you need to check system event) and reference the library containing the task from your application.
For more details you can read this tutorial. Handling background activation from a toast notification using Windows 10 adaptive template should be of particular interest to you.