Toast Notification parameters in Windows Phone 8.1 Silverlight

泪湿孤枕 提交于 2019-12-12 08:27:31

问题


Okay so I'm using the new ToastNotificationManager in my 8.1 SL project instead of the old ShellToast. The ShellToast had NavigationUri on the toast message which made it really easy.

In the new toasts you have to specify the launch parameters by yourself according to this article. However it seems like 8.1 SL doesn't have the event OnLaunched(LaunchActivatedEventArgs args) you are supposed to listen for in App.xaml.cs for the parameters:

Step 2: Handle the app's "OnLaunched" event

When the user clicks on your toast or selects it through touch, the associated app is launched, firing its OnLaunched event.

Note If you do not include a launch attribute string in your toast and your app is already running when the toast is selected, the OnLaunched event is not fired.

This example shows the syntax for the override of the OnLaunched event, in which you will retrieve and act on the launch string supplied through the toast notification.

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    string launchString = args.Arguments

    ....
}

My code:

// 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");

//Launch params
string paramString = "{\"type\":\"toast\",\"param1\":\"12345\"}";
((XmlElement)toastXml.SelectSingleNode("/toast")).SetAttribute("launch", paramString);

// Create the actual toast object using this toast specification.
ToastNotification toast = new ToastNotification(toastXml);

// 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 = true;

// Send the toast.
ToastNotificationManager.CreateToastNotifier().Show(toast);

Anyone knows how to solve this? Thanks


回答1:


Your problem is you're setting the wrong launch parameter. You should set it directly to the page you want to navigate to.

var toastNavigationUriString = ""#/MainPage.xaml?param1=12345";
var toastElement = ((XmlElement)toastXml.SelectSingleNode("/toast"));
toastElement.SetAttribute("launch", toastNavigationUriString);


来源:https://stackoverflow.com/questions/23275723/toast-notification-parameters-in-windows-phone-8-1-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!