locked
How to post a notification to WNS?

    Question

  •     In the sample  "Push Notifications Sample", it shows US how to create a channel for push notification, and how to listen the notification by the channel.

        But I don't know how to send the notification with the channel on the server. I try to code like follows:

     

       private void SendNotificationToWNS()
            {
                XmlDocument tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideImageAndText);
    
                XmlNodeList texts = tileXml.GetElementsByTagName("text");
                for (uint i = 0; i < texts.Length; i++)
                {
                    texts.Item(i).AppendChild(tileXml.CreateTextNode("text"));
                }
                XmlNodeList images = tileXml.GetElementsByTagName("image");
                for (uint i = 0; i < images.Length; i++)
                {
                    XmlElement image = images.Item(i) as XmlElement;
                    image.SetAttribute("src", "http://www.baidu.com/img/baidu_sylogo1.gif");
                    image.SetAttribute("alt", "Baidu");
                }
    
                mRequest = (HttpWebRequest)HttpWebRequest.Create(mChannel.Uri);
                mRequest.Method = "POST";
                //mRequest.ContentType = "application/x-www-form-urlencoded";
                mRequest.BeginGetRequestStream(new AsyncCallback(onConnected), tileXml);
            }
    
            private void onConnected(IAsyncResult ar)
            {
                XmlDocument tileXml = ar.AsyncState as XmlDocument;
    
                Stream stream = mRequest.EndGetRequestStream(ar);
                StreamWriter writer = new StreamWriter(stream);
                writer.Write(Encoding.UTF8.GetBytes(tileXml.GetXml()));
                mRequest.BeginGetResponse(new AsyncCallback(onResponse), mRequest);
            }
    
            private void onResponse(IAsyncResult ar)
            {
                WebResponse responese =  mRequest.EndGetResponse(ar);
                StreamReader reader = new StreamReader(responese.GetResponseStream());
                txtResponese.Text = reader.ReadToEnd();
            }
    

     

     

        Well , it throws an exception on the line" WebResponse responese =  mRequest.EndGetResponse(ar);", which is http400, bad request.

        So, how can I send the notification with channel?

     


    • Edited by NMG1852951 Friday, December 09, 2011 7:25 AM
    Friday, December 09, 2011 7:25 AM

Answers

All replies

  • Hi NMG,

    This shows you an implementation:

    http://blogs.msdn.com/b/jpsanders/archive/2011/12/02/walkthrough-creating-an-iis-server-to-use-with-wns-push-notifications-metro.aspx

    Please read the reference documentation in that post.

    Feel free to use any code from that example if you want.

     

    -Jeff


    Jeff Sanders (MSFT)
    Friday, December 09, 2011 11:43 AM
    Moderator
  •     Thanks for your help. This the second time that you help me.

        It is night now. So I will go to see it tomorrow.

        Thanks again~

    Friday, December 09, 2011 12:41 PM
  •     I find a problem.

        I can access"https://login.live.com" with fireFox, but I can not access it with command ping.

       

        So, when I want to auth to WNS to get access token with webClient.UploadString(), it throw an exception, which is http 400 bad request ?

     

        Maybe you will tell me what I should do to solve this problem. Thanks~

     

     

    Monday, December 12, 2011 7:09 AM
  • Hi NM!

    Ping uses a different port than HTTP and HTTPS.  The fact that you cannot ping out is because something in the network chain does not respond to that report (it could also be the endpoint server).

    The 400 error is coming back from the server so you definately are accessing that server.  The error means that your request is not correct (bad request).

    Did you get this error using my code?

    -Jeff


    Jeff Sanders (MSFT)
    Monday, December 12, 2011 12:40 PM
    Moderator
  •     Here is my code:

       

            public static Token getToken(string sid, string secret)
            {
                //string name = HttpUtility.UrlEncode(packageName);
                //string sec = HttpUtility.UrlEncode(secret);
    
                //string data = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com", name, sec);
    
                //WebClient client = new WebClient();
                ////client.Proxy = new WebProxy("172.29.8.29", 80);
                //client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    
                //string response = client.UploadString(AUTH_URL,data);
    
                //return createTokenFromResponse(response);
    
                var urlEncodedSid = HttpUtility.UrlEncode(sid);
                var urlEncodedSecret = HttpUtility.UrlEncode(secret);
    
                var body =
                  String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com", urlEncodedSid, urlEncodedSecret);
    
                var client = new WebClient();
                client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    
                string response = client.UploadString("https://login.live.com/accesstoken.srf", body);
    
                return null;
            }
    


        Because it is copied from your blog, so I think it is no problem.

        Where may cause problem is my code this website with the debug function of the VS2011, not from IIS. Is it may cause problem?

        On the other hand, is there the possible that network setting may cause HTTP 400 problem?

    Tuesday, December 13, 2011 3:30 AM
  •      OK, I get it.

         I am sorry I forgot to check the input parameters,  they has en space at the end, so login server give me an HTTP400.

         After I add trim to remove the space, it works and I get the accesstoken.

        Thanks for your help~

    Tuesday, December 13, 2011 3:44 AM
  •     ok, I get all I want to know. I update the tile of my app with push notification just now.

        Thanks for your help ~

    Tuesday, December 13, 2011 6:08 AM
  • You are welcome!  Glad you found the problem and shared your solution too!


    Jeff Sanders (MSFT)
    Tuesday, December 13, 2011 1:52 PM
    Moderator
  • Hello NMG,

    I too came across with the same issue. Can you please tell me in detail how did you fix it?

    What input parameters do i need to check? Are they "sid" and "secret"? What is the "space" you have mentioned?

    Thankx a lot :)

    Thursday, March 21, 2013 5:56 AM