sd_dracula
sd_dracula

Reputation: 3896

Windows 10 Universal XAML Toast Notifications

I have Win 10 Universal App which displays toast notifications based on various events within the app.

My problem is that the toast notifications only get displayed when the app is active (it is not minimized to the task bar).

I need the app to display the notifications when I am using any other apps. My settings below: enter image description here

Toast calling code:

private void DisplayNotification()
        {
            string toastXmlString = "<toast>"
                               + "<visual version='1'>"
                               + "<binding template='ToastText04'>"
                               + "<text id='1'>Header</text>"
                               + "<text id='2'>Line 1</text>"
                               + "<text id='3'>Line 2</text>"
                               + "</binding>"
                               + "</visual>"
                               + "</toast>";

            Windows.Data.Xml.Dom.XmlDocument toastDOM = new Windows.Data.Xml.Dom.XmlDocument();
            toastDOM.LoadXml(toastXmlString);

            // Create a toast, then create a ToastNotifier object to show
            // the toast
            ToastNotification toast = new ToastNotification(toastDOM);

            ToastNotificationManager.CreateToastNotifier().Show(toast);
        }

Anyone know why this is?

Upvotes: 2

Views: 2088

Answers (1)

Shawn Kendrot
Shawn Kendrot

Reputation: 12465

You need to create a new BackgroundTask for that. There are many solutions available out there, but here is the short list:

  1. Add a new Windows Runtime Component project to the solution.
  2. In the manifest in declarations add a new background task and select Push notifications and/or Timer.
  3. Set the entry point to be the fully qualified name (namespace.classname)
  4. Register your task at app startup

    if (BackgroundTaskRegistration.AllTasks.Any(task => task.Value.Name == TaskNameConst)) return;

    BackgroundTaskBuilder builder = new BackgroundTaskBuilder(); builder.Name = TaskNameConst; builder.TaskEntryPoint = TaskEntryPointConst; builder.SetTrigger(new TimeTrigger(15, false)); builder.Register();

Upvotes: 3

Related Questions