c# - Push notification to wp8 using javascript (JQuery Request) -


so trying pushing notifications windows 8 application using javascript.

so after checking website useful website found.

http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202977(v=vs.105).aspx

i understood following:

1- need develop app request push notification service uri shared push client service.

(the code app needs nothing copy link above)

2- need send notification (raw notification) using push client service

(here appears problem :) )

the issue code .net files not familiar with. tried convert code .net html , .js going send ajax request given uri.

however give me wired error , unfortunately couldn't find example of sending notification using .js

so here original code defines simple form send request uri. difference between , code using jquery ajax request send

original code

html

<%@ page language="c#" autoeventwireup="true" codebehind="sendraw.aspx.cs" inherits="sendraw.sendraw" %>  <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"     "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml"> <head id="head1" runat="server">     <title></title> </head> <body>     <form id="form1" runat="server">    <div>      <br />     enter uri:</div> <asp:textbox id="textboxuri" runat="server" width="666px"></asp:textbox> <br /> <br /> enter value 1:<br /> <asp:textbox id="textboxvalue1" runat="server"></asp:textbox> <br /> <br /> enter value 2:<br /> <asp:textbox id="textboxvalue2" runat="server"></asp:textbox> <br /> <br /> <br /> <asp:button id="buttonsendraw" runat="server" onclick="buttonsendraw_click"      text="send raw notification" /> <br /> <br /> response:<br /> <asp:textbox id="textboxresponse" runat="server" height="78px" width="199px"></asp:textbox> </form> 

c# (.net)

 using system.net;  using system.io;  using system.text; 

c# (.net)

protected void buttonsendraw_click(object sender, eventargs e)     {         try         {             // uri microsoft push notification service returns push client when creating notification channel.             // normally, web service listen uris coming web client , maintain list of uris send             // notifications out to.             string subscriptionuri = textboxuri.text.tostring();               httpwebrequest sendnotificationrequest = (httpwebrequest)webrequest.create(subscriptionuri);              // create httpwebrequest posts raw notification microsoft push notification service.             // http post method allowed send notification.             sendnotificationrequest.method = "post";              // create raw message.             string rawmessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +             "<root>" +                 "<value1>" + textboxvalue1.text.tostring() + "<value1>" +                 "<value2>" + textboxvalue2.text.tostring() + "<value2>" +             "</root>";              // set notification payload send.             byte[] notificationmessage = encoding.default.getbytes(rawmessage);              // set web request content length.             sendnotificationrequest.contentlength = notificationmessage.length;             sendnotificationrequest.contenttype = "text/xml";             sendnotificationrequest.headers.add("x-notificationclass", "3");               using (stream requeststream = sendnotificationrequest.getrequeststream())             {                 requeststream.write(notificationmessage, 0, notificationmessage.length);             }              // send notification , response.             httpwebresponse response = (httpwebresponse)sendnotificationrequest.getresponse();             string notificationstatus = response.headers["x-notificationstatus"];             string notificationchannelstatus = response.headers["x-subscriptionstatus"];             string deviceconnectionstatus = response.headers["x-deviceconnectionstatus"];              // display response microsoft push notification service.               // normally, error handling code here. in real world, because data connections not available,             // notifications may need throttled if device cannot reached.             textboxresponse.text = notificationstatus + " | " + deviceconnectionstatus + " | " + notificationchannelstatus;         }         catch (exception ex)         {             textboxresponse.text = "exception caught sending update: " + ex.tostring();         } 

my code

<html xmlns="http://www.w3.org/1999/xhtml"> <head id="head1">     <script src="jquery-2.0.3.min.js"></script>     <script>      $(document).ready(function(e) {         $('#buttonsendraw').click(function(){         console.log("button pressed");          var subscriptionuri = $('#textboxuri').val();         var textbox1 = $('#textboxvalue1').val();         var textbox2 = $('#textboxvalue2').val();          var rawmessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +         "<root>" +             "<value1>" + textbox1 + "<value1>" +             "<value2>" + textbox2 + "<value2>" +         "</root>";         console.log("sending data");         $.ajax({             url: subscriptionuri,             type: "post",             data: rawmessage,             contenttype:"text/xml",             success: function(response) {                console.log('sucess');                console.log("login| response");                console.log(response);               },              error: function (xhr, ajaxoptions, thrownerror) {                     console.log(xhr.status);                     console.log(thrownerror);             }              });         });     });   </script>     </head>     <body>     enter uri:<input type="text" id="textboxuri" width="666px" /> <br />  enter value 1: <input type="text" id="textboxvalue1" /> <br />  enter value 2: <input id="textboxvalue2" /> <br />  <button id="buttonsendraw">sending data</button> <br />  response: <input id="textboxresponse" height="78px" width="199px" /> <br />    </body>   </html> 

the error getting is:

    405 (method not allowed) 

where method set automatically options, although sending post request

any suggestions, in advance :)

i'm assuming subscriptionuri value uri different domain website contains page jquery code. (e.g. website hosted on www.mywebsite.com , subscriptionuri www.microsoft.com/api/push)

what happens in scenario browser attempt make cross domain request. there security considerations keep in mind when using cross domain requests cross-site request forgery or cross-site scripting. see here more information.

as rule, modern browsers allow ajax calls uris in same domain html page. if uri in other domain, browser won't make direct call. instead, try make cors request.

cross origin resource sharing (cors) specification provides mechanism web application developers have browser-supported mechanism make xmlhttprequests domain in secure manner. cors mechanism works adding http headers cross-domain http requests , responses. these headers indicate origin of request , server must indicate via headers in response whether serve resources origin. exchange of headers makes cors secure mechanism. server must support cors , indicate domain of client making request permitted so. beauty of mechanism is automatically handled browser , web application developers not need concern details.

there different flows in cors simple request, preflighted requests , credentialed requests. request trying make fall in category of preflighted request because request body has content-type other text/plain, application/x-www-form-urlencoded, or multipart/form-data.

options http://otherdomain.com/some-resource/ http/1.1 origin: http://mydomain.com access-control-request-method: post access-control-request-headers: x-foo 

this receive following response server (if cors enabled on server) indicating post operation allowed

http/1.1 200 ok access-control-allow-origin: http://mydomain.com access-control-allow-methods: get, post, put, delete, options access-control-allow-headers: x-foo access-control-max-age: 3600 

once response received, browser make actual post.

put http://otherdomain.com/some-resource/ http/1.1 content-type: application/xml; charset=utf-8 x-foo: bar 

this process ensures among other things servers not cors-enabled not process request might modify server resources side effect prior browser disallowing response because lacks proper access-control-allow-origin header.

in case since server not cors enabled seeing options call not subsequent post. easiest way resolve enable cors on server necessary headers can sent server in response. hope helps!


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -