In some cases, tile push is insufficient to meet our needs. For example, we need to remind users that there will be heavy rain or flight delays in the next three hours.
If you want to use toast push, it must be enabled in the configuration file.
However, you can also manually disable pushing. In this case, we can do nothing about it. Therefore, you need to check whether pushing is allowed before enabling pushing.
Toastnotifier; private void gettileupdate () {tilenotifier = tileupdatemanager. createtileupdaterforapplication (); If (tilenotifier. Setting! = Icationicationsetting. enabled) {var dialog = new messagedialog ("enable push notification"); dialog. showasync ();}}
The essence of toast is the same as the tile we mentioned earlier. The encapsulated XML is used for execution, but the representation is different. Therefore, the code of toast is similar.
However, there are several significant differences.
1. the push of toast can display up to three at the same time, but the number is not limited. After the previous display is complete, it will be added later.
2. The sound can be displayed when the toast is pushed
See more http://msdn.microsoft.com/en-us/library/br230842.aspx
1. Simple toast
var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
var tileAttributes = template.GetElementsByTagName("text")[0];
tileAttributes.AppendChild(template.CreateTextNode("That's tile test 1 !"));
var notifications1 = new ToastNotification(template);
toastNotifier.Show(notifications1);
Similarly, if you are familiar with the XML structure, you can directly use XML
var xml = @"<toast>
<visual lang='en-US'>
<binding template='ToastText01'>
<text id='1'>test 2</text>
</binding>
</visual>
</toast>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
var notifications = new ToastNotification(xmlDoc);
toastNotifier.Show(notifications);
2. short code is equivalent.
2. timed toast and Cancellation
var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
var tileAttributes = template.GetElementsByTagName("text")[0];
tileAttributes.AppendChild(template.CreateTextNode("That's tile shedule toast notifications !"));
var data = DateTimeOffset.Now.AddSeconds(2);
var stn = new ScheduledToastNotification(template, data);
toastNotifier.AddToSchedule(stn);
Note that the time type here is
Datetimeoffset
IReadOnlyList<ScheduledToastNotification> scheduled = notifier.GetScheduledToastNotifications();
We can use getscheduledtoastnotifications to obtain the toast that is waiting for pushing, and then use the removefromschedule method to remove the specified toast push.
notifier.RemoveFromSchedule(ScheduledToastNotification);
3. The toast display time with parameters is subject to system restrictions.
If we want to display toast for a longer time, we can use code to set the Duration Attribute, which can be extended to 25 s.
var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
var tileAttributes = template.GetElementsByTagName("text")[0];
tileAttributes.AppendChild(template.CreateTextNode("That's tile toast notifications !"));
var launch = template.CreateAttribute("launch");
launch.Value = "aaaaa";
template.FirstChild.Attributes.SetNamedItem(launch);
var duration = template.CreateAttribute("duration");
duration.Value = "long";
template.FirstChild.Attributes.SetNamedItem(duration);
var t = template.GetXml();
var notifications = new ToastNotification(template);
notifications.Activated += notifications_Activated;
toastNotifier.Show(notifications);
Through launch, we can pass parameters. Through duration, we can set the time duration to be divided into short (default) and long
Through the introduction in the previous article and this article, you have mastered enough local push skills ~ Enjoy
Code download