11

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

csharpwinphonexaml
  • 3,659
  • 10
  • 32
  • 63
robertk
  • 2,461
  • 1
  • 27
  • 39
  • You can provide the toast with a navigation parameter directly. I'll get the details when I get back to work tomorrow. Odd we haven't documented this properly. – Claus Jørgensen Apr 24 '14 at 20:17
  • Thanks looking forward to it! :) – robertk Apr 25 '14 at 06:42
  • If you are using the ToastNotificationManager in Silverlight 8.1, what are you using instead of the OnLoaded event since SL doesn't have that in the App.xaml? I have it in the OnNavigatedTo, but it seems to call it twice when the toast is clicked I use the answer for the load trigger below.?? – gcoleman0828 Jun 09 '15 at 02:15

1 Answers1

10

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);
Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
  • 1
    Where do you need to define `protected override void OnLaunched(LaunchActivatedEventArgs args)` method in WP 8.1 SilverLight app? (In App.xaml.cs it says no method to override) – Shishir Gupta Jun 21 '14 at 14:13
  • 3
    @ShishirGupta You don't, that method is used exclusively for Universal Apps. For Silverlight 8.1, you just pass it a full uri, directly to the page you want to enter, as per my example. – Claus Jørgensen Jun 21 '14 at 14:52
  • @ClausJørgensen What if we don't want to navigate to any page but want to send some data using toast ? – loop Dec 08 '14 at 15:36
  • @loop You must *always* navigate to a page in your app when a toast is clicked. Otherwise it won't pass certification. – Claus Jørgensen Dec 08 '14 at 15:52
  • @ClausJørgensen thanks Claus , one more thing - data should be passed in the uri as parameter right ? – loop Dec 08 '14 at 16:02
  • Given it's cross-process, that's effectively your only option. – Claus Jørgensen Dec 08 '14 at 16:03
  • How to handle in JavaScript or Windows Runtime Component class in Windows Phone 8.1? – Kishor Bikram Oli Aug 09 '15 at 03:27