在Windows Phone 8.1 Silverlight中从后台代理发送静默Toast

时间:2014-04-23 07:18:30

标签: c# windows-phone windows-phone-8.1

从我的WP8后台代理程序中,我得到了正常的ShellToast,它运行正常。

但是现在使用WP8.1我希望能够在某些时间(夜晚)发送安静的吐司,它应该只在这些时间内出现在通知中心。 我一直关注this guide,但它似乎根本不起作用。吐司没有露面......

任何有这个工作的人呢?

由于

我的代码:

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);
}

2 个答案:

答案 0 :(得分:4)

您需要使用新的 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 ,否则您的应用不会通过认证。我花了大约一天时间来解决这个问题,但这并不是很明显。

答案 1 :(得分:2)

谢谢你们!我错过了8.1 SL附带的新Package.appmanifest。旧的ShellToast似乎没有它(我认为?),但没有新的Toast名称空间。

enter image description here

相关问题