[英]Send silent toast from background agent in Windows Phone 8.1 Silverlight
從我的WP8后台代理中,我得到了正常運行的ShellToast。
但是現在有了WP8.1,我希望能夠在某些小時(晚上)內發送安靜的吐司,它應該僅在這些小時內顯示在通知中心中。 我一直在遵循本指南,但它似乎根本不起作用。 吐司沒有出現...
有人讓它起作用了嗎?
謝謝
我的代碼:
public MainPage()
{
InitializeComponent();
SendToast();
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
SendToast();
}
private void SendToast()
{
// Using the ToastText02 toast template.
ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
// Retrieve the content part of the toast so we can change the text.
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
//Find the text component of the content
XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
// Set the text on the toast.
// The first line of text in the ToastText02 template is treated as header text, and will be bold.
toastTextElements[0].AppendChild(toastXml.CreateTextNode("Heading"));
toastTextElements[1].AppendChild(toastXml.CreateTextNode("Body"));
// Set the duration on the toast
IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
((XmlElement)toastNode).SetAttribute("duration", "long");
// Create the actual toast object using this toast specification.
ToastNotification toast = new ToastNotification(toastXml);
toast.ExpirationTime = DateTimeOffset.UtcNow.AddSeconds(3600);
// Set SuppressPopup = true on the toast in order to send it directly to action center without
// producing a popup on the user's phone.
toast.SuppressPopup = false;
// Send the toast.
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
您需要使用新的Windows.UI.Notifications.ToastNotification API。
示例,如何使用它在這里:
http://code.msdn.microsoft.com/wpapps/Action-Center-Quickstart-b15089f2
快速入門文檔在這里:
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn631259.aspx
如果要發送靜默通知,只需將SuppressPopup屬性設置為true:
http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.notifications.toastnotification.suppresspopup.aspx
重要說明 -如果要在Silverlight 8.1應用程序中使用此API,則需要將WMAppManifest.xml中的通知類型更改為WNS ,否則您的應用程序將無法通過認證。 我花了一天的時間解決這個問題,這並不明顯。
多謝你們! 我錯過了8.1 SL附帶的新Package.appmanifest。 沒有它,舊的ShellToast似乎可以工作(我想呢?),但新的Toast命名空間則不能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.