Domanda

Im ottenendo il seguente errore ,quando io uso il seguente codice per inviare le notifiche push per windows phone.

Una notifica push payload di errore si è verificato.L'XML non validi o non correttamente formattato in XML o il tipo di notifica specificato nell'intestazione non corrisponde al tipo di payload utilizzato.

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();
   }

Ho usato il codice C# da questo link come riferimento http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202967%28v=vs.105%29.aspx

È stato utile?

Soluzione

Sei la conversione di una stringa di byte array, utilizzando la codifica UTF-8.Quindi si crea un flusso di scrittore, con la codifica UTF-16, convertire il valore del byte della stringa, e scriverle nel flusso.Io onestamente non vedo come potrebbe funzionare.

Invece, provare questo:

    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();

Se non riesci a passare una Stringa per il write il metodo, quindi scrivere direttamente nel flusso di output:

     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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top