Search code examples
c#uwptoasttemplate10wns

Retrieve toast arguments from within a background task using ToastNotificationActionTrigger


I am trying to retrieve the arguments from a toast notification within a background task, however the recommended method on Microsofts website isn't working.

I am registering the task as below within the OnInitialize() method of my main UWP application as below:

foreach (var task in BackgroundTaskRegistration.AllTasks)
{
      if (task.Value.Name.Equals(taskName))
      {
           task.Value.Unregister(true);
      }
}

BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();

BackgroundTaskBuilder builder = new BackgroundTaskBuilder()
{
      Name = taskName,
      TaskEntryPoint = "AppNamespace.GetQuestionsBackground"
};

The toast notification that I am sending to the device is as follows:

var toast = "<?xml version=\"1.0\" encoding=\"utf - 8\"?>" +
            $"<toast launch=\"questionId:{question.ToString()}:{journal.Id.ToString()}:{staffId.ToString()}\">" +
            "<visual><binding template=\"ToastImageAndText02\">" +
            "<text id=\"1\">New Question</text>" +
            "<text id=\"2\">There is a question waiting to be answered</text>" +
            "<image id=\"1\" src=\"ms-appx:///Assets/DailyQuestion32.png\" />" +
            "</binding></visual>" +
            "<actions>" +
            $"<action activationType=\"foreground\" content=\"Answer now\" arguments=\"now:{question.ToString()}:{journal.Id.ToString()}:{staffId.ToString()}\"/>" +
            $"<action activationType=\"background\" content=\"Answer later\" arguments=\"later:{question.ToString()}:{journal.Id.ToString()}:{staffId.ToString()}\"/>" +
            "</actions>" +
            "</toast>";

And the background task (which is in a separate, windows runtime component project) is as follows:

public sealed class GetQuestionsBackground : IBackgroundTask
{
    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        _deferal = taskInstance.GetDeferral();

        this.container = this.diBootStaraaper.BootStrap(applicationData);

        var details = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail;

        if (details.Argument != null)
        {
            var argList = details.Argument.Split(':');

            IDataProvider dataProvider = DependencyHelper.Resolve<IDataProvider>();
            await dataProvider.SaveQuestion(argList[1], argList[2], argList[3]);
            dataProvider.SaveSettings();

        }
        _deferal.Complete();
    }
}

The package manifest also has the background task registerd as follows:

<Extension Category="windows.backgroundTasks" EntryPoint="AppNamespace.GetQuestionsBackground">
      <BackgroundTasks>
        <Task Type="systemEvent" />
      </BackgroundTasks>
</Extension>

The details variable is simply empty howeverm the if statement condition is true? Furthermore, visual studio is unable to view the current state of the taskInstance.triggerDetails when I breakpoint into the code and step through it...

Any help would be appreciated, thank you!

UPDATE: I have breakpointed into the background task when I click on the toast notification button and viewed the variables in Visual Studio, and I can see the data I want under the m_argument variable within the _PiEventDetails on the Native Implementation. So I know the data is there but the getter doesn't seem to be working.


Solution

  • So it turns out that I was in fact trying to do:

        var arguments = details.argument;
        arguments.split(':');
    

    instead of trying to access details.argument directly, and this was apparently invalid.

    Thanks @jay-zuo-msft for helping me realise this.