Reputation: 23
I am sending a toast message using a background task in a UWP application. The code is successfully getting executed but it isn't sending the toast message. I tried many ways but none of them is working.
This is for a UWP application.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.ApplicationModel.Background;
using Windows.UI.Notifications;
namespace BackgroundStuff
{
public sealed class MyBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
SendToast("Hi this is background Task");
Debug.WriteLine("Hi this is background Task");
}
public static void SendToast(string message)
{
//var template = ToastTemplateType.ToastText01;
//var xml = `ToastNotificationManager.GetTemplateContent(template);`
//var elements = xml.GetElementsByTagName("text");
//var text = xml.CreateTextNode(message);
//elements[0].AppendChild(text);
//var toast = new ToastNotification(xml);
//ToastNotificationManager.CreateToastNotifier().Show(toast);
var xmdock = CreateToast();
var toast = new ToastNotification(xmdock);
var notifi = `Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();`
notifi.Show(toast);
}
public static Windows.Data.Xml.Dom.XmlDocument CreateToast()
{
var xDoc = new XDocument(
new XElement("toast",
new XElement("visual",
new XElement("binding", new XAttribute("template", `"ToastGeneric"),`
new XElement("text", "C# Corner"),
new XElement("text", "Do you got MVP award?")
)
),// actions
new XElement("actions",
new XElement("action", new XAttribute("activationType", `"background"),`
new XAttribute("content", "Yes"), new XAttribute("arguments", `"yes")),`
new XElement("action", new XAttribute("activationType", `"background"),`
new XAttribute("content", "No"), new XAttribute("arguments", `"no"))`
)
)
);
var xmlDoc = new Windows.Data.Xml.Dom.XmlDocument();
xmlDoc.LoadXml(xDoc.ToString());
return xmlDoc;
}
}
}
I expected a toast message but I couldn't get it.
Upvotes: 1
Views: 782
Reputation:
you can create a common function -
using Windows.UI.Notifications;
public static void ShowToastNotification(string title, string stringContent)
{
ToastNotifier ToastNotifier = ToastNotificationManager.CreateToastNotifier();
Windows.Data.Xml.Dom.XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
Windows.Data.Xml.Dom.XmlNodeList toastNodeList = toastXml.GetElementsByTagName("text");
toastNodeList.Item(0).AppendChild(toastXml.CreateTextNode(title));
toastNodeList.Item(1).AppendChild(toastXml.CreateTextNode(stringContent));
Windows.Data.Xml.Dom.IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
Windows.Data.Xml.Dom.XmlElement audio = toastXml.CreateElement("audio");
audio.SetAttribute("src", "ms-winsoundevent:Notification.SMS");
ToastNotification toast = new ToastNotification(toastXml);
toast.ExpirationTime = DateTime.Now.AddSeconds(4);
ToastNotifier.Show(toast);
}
Upvotes: 1
Reputation: 7737
The process of creating an XML document does not reflect the hierarchical relationship between the tags, so the software cannot treat it as a Toast XML document that can be parsed.
Toast Content in UWP is based on XML, but manipulating XML in C# is not as convenient as operating classes, so there is now a new way to build Toast Content.
You can add the Microsoft.Toolkit.Uwp.Notifications
nuget package in your project, then create a toast content like this:
var toastContent = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = "Adaptive Tiles Meeting",
HintMaxLines = 1
},
new AdaptiveText()
{
Text = "Conf Room 2001 / Building 135"
},
new AdaptiveText()
{
Text = "10:00 AM - 10:30 AM"
}
}
}
var toast = new ToastNotification(toastContent.GetXml());
ToastNotificationManager.CreateToastNotifier().Show(toast);
Here is the document.
Best regards.
Upvotes: 1