locked
[U8.1] Sending custom parameter with toast notification RRS feed

  • Question

  • Hi, I'm trying to send a custom parameter with a toast notification to my universal 8.1 app. It works perfectly with Windows store app, showing correct icon, and the text TestMessage, but in Windows Phone it shows and empty square icon, correct appname and the text New Notification. If I remove the messtype element, it works as it should, but I want to send a custom parameter with the toast.
    I also tried to move the messtype element from the visual to the toast element, and then it works in WP, but not in Windows store apps. How to do this the correct way?

    <toast>
     <visual>
      <binding template="ToastImageAndText01">
       <image id="1"src="http://mikla.nu/small.png" /> 
       <text id="1">TestMessage</text> 
       <messtype>Chat</messtype> 
      </binding>
     </visual>
    </toast>
    BR /Magnus
    • Edited by Fred Bao Wednesday, November 18, 2015 6:47 AM add the tag
    Tuesday, November 17, 2015 11:47 PM

Answers

  • Hi,

    I found a solution that worked:

    <toast>
     <commands>
      <command arguments="Chat" />
     </commands>
     <visual>
      <binding template="ToastText01">
       <text id="1">TestMessage</text>
      </binding>
     </visual>
    </toast>

    And then I can look for the value in the client by:

    IXmlNode xn = args.ToastNotification.Content.GetElementsByTagName("command")[0];
    string MessType = xn.Attributes[0].NodeValue.ToString();
    /Magnus
    • Marked as answer by Flowhill1 Wednesday, November 18, 2015 10:44 PM
    Wednesday, November 18, 2015 10:43 PM

All replies

  • Hello,

    >>but in Windows Phone it shows and empty square icon, correct appname and the text New Notification.

    This is by designed behavior according to MSND document:

    Remarks

    Note  You can send any toast template to Windows Phone 8.1, but it will be rendered as a modified version of ToastText02, with no image, two text strings (both on the same line, the first string in bold), and the app's Square 150x150 logo.

    So I afraid with the windows phone 8.1, it is not possible to send a custom parameter.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 18, 2015 7:09 AM
  • Hi,

    Thanks for your reply. I have tried ToastText02 also. The behavior is as expected without the messtype element. But I don't understand why I cannot add a custom element? Why should WP/Windows care? The <messtype> element in <toast> element makes it not working in Windows, and <messtype> in <visual><binding> element makes it not working in WP.

    /Magnus

    Wednesday, November 18, 2015 9:47 AM
  • Hi,

    I found a solution that worked:

    <toast>
     <commands>
      <command arguments="Chat" />
     </commands>
     <visual>
      <binding template="ToastText01">
       <text id="1">TestMessage</text>
      </binding>
     </visual>
    </toast>

    And then I can look for the value in the client by:

    IXmlNode xn = args.ToastNotification.Content.GetElementsByTagName("command")[0];
    string MessType = xn.Attributes[0].NodeValue.ToString();
    /Magnus
    • Marked as answer by Flowhill1 Wednesday, November 18, 2015 10:44 PM
    Wednesday, November 18, 2015 10:43 PM
  • Hi Magnus,

    Do NOT add your own elements or arguments to the toast payload. This will potentially break your toast notification randomly. We can only guarantee that the schema we specify will continue working. That means in a future update we might decide to make the parser more strict and your <commands> element will break toast notifications in a future update.

    Instead, use the launch argument on the <toast> element to provide custom data that your app can use later to perform the correct action. The launch argument is returned when the user clicks on your toast, so that you can navigate to the correct content. You didn't describe your scenario, but I'm guessing this is likely what you want it for. Plus, we guarantee that the launch attribute will be preserved and valid, whereas your random elements are very dangerous.

    Read the How to handle activation from a toast notification documentation to learn more.

    Here's how the launch attribute works in toasts using the legacy 8.1 XML templates (like ToastText01 as you're using)...

    1. When generating the XML, you provide the sufficient data you need inside the launch attribute of the <toast> element. Serialize this data as a string (and of course make sure it's XML escaped).

    2. When the user clicks on your toast, your app's OnLaunched method (in App.xaml.cs) is called, and the LaunchActivatedEventArgs object has an Arguments property, which contains your launch string specified in step 1. You can then decide where to navigate in your app based on that.

    Here's how your toast should look...

    <toast launch="Chat">
     <visual>
      <binding template="ToastImageAndText01">
       <image id="1"src="http://mikla.nu/small.png" /> 
       <text id="1">TestMessage</text> 
      </binding>
     </visual>
    </toast>

    And here's what your App.xaml.cs OnLaunched method would look like...

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        e.Arguments; // Your toast's launch argument is here
    }

    • Proposed as answer by andrewbares7 Thursday, November 19, 2015 7:19 PM
    Thursday, November 19, 2015 7:18 PM