WindowsPhoneDev WindowsPhoneDev - 6 days ago 6
C# Question

Set parameter to notification

I'm using C# and Windows Phone 8.1 as Universal app. I can send notifications from background by this code:

void ShowNotification(string title, string text)
{
ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;

XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
XmlNodeList textElements = toastXml.GetElementsByTagName("text");
textElements[0].AppendChild(toastXml.CreateTextNode(title));
textElements[1].AppendChild(toastXml.CreateTextNode(text));

ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));
}


I wanna when user tapped on these notifications in action center, that message I wrote in notification shows on a textBlock in my MainPage but I don't know how can I set a parameter to do this. I used this code in MainPage but parameter is empty:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e != null && e.Parameter != null)
{
Debug.WriteLine("MainPage.OnNavigatedTo.Parameter: " + e.Parameter.ToString());
}
}


thanks

Answer

You need to specify app launch parameters like:

IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
((XmlElement)toastNode).SetAttribute("launch", "{\"type\":\"toast\",\"param1\":\"12345\",\"param2\":\"67890\"}");

Then in app's onlaunched event, navigate to mainpage with the parameter you get as below:

rootFrame.Navigate(typeof(MainPage), args.Arguments);