Toast Notification From Background Task

899 Views Asked by At

I work on universal app that one of it's features is to send a notification to the user after a specific time he/she want . Notification appear when Set The System Event (i.e disable and enable wifi) as Follow

builder.SetTrigger(new SystemTrigger(SystemTriggerType.NetworkStateChange, false));

but when work with TimeTrigger The Notification Doesn't appear ! Here is my code :

 var builder = new BackgroundTaskBuilder();
        builder.Name = "MyBackgroundTask";
        builder.TaskEntryPoint = "SampleWindowsStoreApp.BackgroundTask.MyBackgroundTask";

        var _taskbuilder = new TimeTrigger(20, false);
        builder.SetTrigger(_taskbuilder);
        builder.Register();

knowing that I enable Toast From Package.appxmanifest and declare Timer and backgroundTask

It's my BackgroundTask code

 public sealed class MyBackgroundTask : IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        var deferral = taskInstance.GetDeferral();
        ToastNotificationUtilities.ShowMessage("Hello from the background task. ");
        deferral.Complete();
    }
}

I want to know why the notification that should appear after 20 min doesn't appear .!

1

There are 1 best solutions below

0
On BEST ANSWER

Maybe you must use something like that

ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;

XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate); XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode("Hello from the background task. ")); ToastNotification toast = new ToastNotification(toastXml); ToastNotificationManager.CreateToastNotifier().Show(toast);

instand of: ToastNotificationUtilities.ShowMessage("Hello from the background task. ")