Question

Im getting the following error ,when I use the following code to send push notifications to windows phone.

A push notification payload error has occurred.The XML contains invalid or improperly formatted XML or the notification type specified in the header doesn't match the payload type used.

try {
            String channelUri = "http://db3.notify.live.net/throttledthirdparty/01.00/AQFrOsAuKMIrQ6_3k_u4ZLo5AgAAAAADAQAAAAQUZm52OkJCMjg1QTg1QkZDMkUxREQFBkVVTk8wMQ";
            URL url = new URL(channelUri);
        URLConnection uc = url.openConnection();

        String name="hello";
        String body="from junit ";

         String toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                    "<wp:Notification xmlns:wp=\"WPNotification\">" +
                       "<wp:Toast>" +
                            "<wp:Text1>" + name + "</wp:Text1>" +
                            "<wp:Text2>" + body + "</wp:Text2>" +
                            "<wp:Param>/Page2.xaml?NavigatedFrom=Toast Notification</wp:Param>" +
                       "</wp:Toast> " +
                    "</wp:Notification>";

        byte[] mesg = toastMessage.getBytes("UTF-8");


        uc.setRequestProperty("ContentType", "text/xml");
        uc.setRequestProperty("X-WindowsPhone-Target", "toast");
        uc.setRequestProperty("X-NotificationClass", "2");

        uc.setDoOutput(true);
        uc.setDoInput(true);

        OutputStreamWriter writer = new OutputStreamWriter(uc.getOutputStream(),"utf-8");
        writer.write(mesg.toString(),0,mesg.toString().length());
        uc.connect();

        Map<String, List<String>> map = uc.getHeaderFields();
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            System.out.println("Key : " + entry.getKey() + 
                     " ,Value : " + entry.getValue());
        }

        writer.flush();
        writer.close();

        String res = this.getURLOutput(uc);
        System.out.print(res);

    } catch(Exception e) {
       System.out.println(e.getMessage());
       Assert.fail();
   }

I have used the C# code from this link as reference http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202967%28v=vs.105%29.aspx

Was it helpful?

Solution

You're converting your string to byte array, using UTF-8 encoding. Then you create a stream writer, with UTF-16 encoding, convert the value of your bytes to string, and write them into the stream. I honestly don't see how it could work.

Try this instead:

    String toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<wp:Notification xmlns:wp=\"WPNotification\">" +
                   "<wp:Toast>" +
                        "<wp:Text1>" + name + "</wp:Text1>" +
                        "<wp:Text2>" + body + "</wp:Text2>" +
                        "<wp:Param>/Page2.xaml?NavigatedFrom=Toast Notification</wp:Param>" +
                   "</wp:Toast> " +
                "</wp:Notification>";

    // Do your stuff, initialize the headers, and so on... 

    OutputStreamWriter writer = new OutputStreamWriter(uc.getOutputStream(), "utf-8");
    writer.write(toastMessage, 0, toastMessage.length());
    uc.connect();

If you can't pass a String to the write method, then write directly in the output stream:

     String toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<wp:Notification xmlns:wp=\"WPNotification\">" +
                   "<wp:Toast>" +
                        "<wp:Text1>" + name + "</wp:Text1>" +
                        "<wp:Text2>" + body + "</wp:Text2>" +
                        "<wp:Param>/Page2.xaml?NavigatedFrom=Toast Notification</wp:Param>" +
                   "</wp:Toast> " +
                "</wp:Notification>";

    byte[] mesg = toastMessage.getBytes("UTF-8");

    uc.setDoOutput(true);
    uc.setDoInput(true);

    uc.getOutputStream().write(mesg, 0, mesg.length());
    uc.connect();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top