Kishor Bikram Oli
Kishor Bikram Oli

Reputation: 1828

Send Push notifications (Toasts) with launch parameters in Windows 8.1

I have a WinJS project which has a BackgroundTask in Runtime Component that triggers when Push Notification is sent (Raw notifications) from my own webservice. And that Background service creates a local toasts and show it in action notification center.

 public static void ShowNotification(int notificationId, string ToastTitle, int messageType, string messageDetails)
    {
        string messageText = String.Empty;
        ToastTemplateType toastTemplate = ToastTemplateType.ToastText04;
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
        XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");

        toastTextElements[0].AppendChild(toastXml.CreateTextNode(ToastTitle));//Toast notification title
        toastTextElements[1].AppendChild(toastXml.CreateTextNode(messageText));
        toastTextElements[2].AppendChild(toastXml.CreateTextNode(messageDetails));

        var launchAttribute = toastXml.CreateAttribute("launch");
        IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
        ((XmlElement)toastNode).SetAttribute("duration", "short");
        toastNode.Attributes.SetNamedItem(launchAttribute);

        //Launch params
        var toastNavigationUriString = messageDetails;
        var toastElement = ((XmlElement)toastXml.SelectSingleNode("/toast"));
        toastElement.SetAttribute("launch", toastNavigationUriString);

        ToastNotification toast = new ToastNotification(toastXml);
        toast.Tag = notificationId.ToString();
        toast.ExpirationTime = DateTimeOffset.UtcNow.AddDays(3);

        if (true)
        {
            toast.SuppressPopup = false;//to send notification directly to action center without displaying a popup on phone.
        }

        ToastNotificationManager.CreateToastNotifier().Show(toast);
    }

And I was handling those toasts like this in JS:

WinJS.Application.addEventListener("activated", onActivatedHandler, true);

function onActivatedHandler(args) {
    if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
        messageDetails = args.detail.arguments;
        PhonegapService.setNotificationMessage(messageDetails, function () {
            window.location.href = "index.html";
        });
    }
}

XML format that is used in this case on my webservice is:

string rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<root>" +
                "<Value1>" + "Hello" + "<Value1>" +
                "<Value2>" + "Raw" + "<Value2>" +
            "</root>";

Now there is slightly change in Push Notifications. I want to send push notifications (Toasts) directly from my webservice rather than sending Raw messages.

My questions are:

  1. How to attach launch params and message on my toast notification in web-service similar like we did while creating local toasts.
  2. When toasts are received how to handle click events and get the useful messages attached with that notification.

XML in this case would be like this:

string toast1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?> ";

           string toast2 = string.Format(@"<toast>
                       <visual>
                           <binding template=""ToastText04"">
                               <text id=""1"">{0}</text>
                               <launch></launch>
                           </binding>
                       </visual>
                   </toast>",message);
           string xml = toast1 + toast2;

Update 1

I used the following XML on my web-service. But I'm getting notification in an unusual format than I expected:

 string toast1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?> ";
 string message = "some json";
                string toast2 = string.Format(@"<toast launch= ""{0}"">
                         <visual version=""1"">
                             <binding template=""ToastText02"">
                                 <text id=""1"">{1}</text>
                                   <text id=""2"">{2}</text>
                             </binding>
                         </visual>
                     </toast>", message, "Alert", "Test");

Upvotes: 1

Views: 1355

Answers (1)

Kristian Vukusic
Kristian Vukusic

Reputation: 3324

You need to create an XML with the same structure as the following (with or without the custom audio):

<toast launch=\"$param\">
   <audio src=\"ms-appx:///Assets/Sounds/$sound.wav\"/>
   <visual>
       <binding template=\"ToastText04\">
           <text id=\"1\">$title</text>
           <text id=\"2\">$msg</text>
       </binding>
   </visual>
</toast>

Notice that launch is a member of the <toast> tag.

You can handle click events the same as before when the app is activated, you get the string that is the value of launch.

Upvotes: 1

Related Questions