在黑莓的C#中推送通知示例

时间:2012-11-21 10:10:17

标签: c# blackberry

  1. 在服务器端推送通知示例为C#我已经拥有此代码:
  2. [WebMethod]  public bool push(string notification) 
    {
        bool success = true;
        byte[] bytes = Encoding.ASCII.GetBytes("Hello");
    
        Stream requestStream = null;
        HttpWebResponse HttpWRes = null;
        HttpWebRequest HttpWReq = null;
    
        String BESName = "cp****.pushapi.eval.blackberry.com";
        try
        {
            //http://<BESName>:<BESPort>/push?DESTINATTION=<PIN/EMAIL>&PORT=<PushPort>&REQUESTURI=/
            // Build the URL to define our connection to the BES.
            string httpURL = "https://" + BESName +  "/push?DESTINATION=2B838E45&PORT=32721&REQUESTURI=/";
    
            //make the connection
            HttpWReq = (HttpWebRequest)WebRequest.Create(httpURL);
            HttpWReq.Method = ("POST");
            //add the headers nessecary for the push
            HttpWReq.ContentType = "text/plain";
            HttpWReq.ContentLength = bytes.Length;
            // ******* Test this *******
            HttpWReq.Headers.Add("X-Rim-Push-Id", "2B838E45" + "~" + DateTime.Now); //"~" +pushedMessage +
            HttpWReq.Headers.Add("X-Rim-Push-Reliability", "application-preferred");
            HttpWReq.Headers.Add("X-Rim-Push-NotifyURL", ("http://" + BESName + "2B838E45~Hello~" + DateTime.Now).Replace(" ", ""));
    
            // *************************
            HttpWReq.Credentials = new NetworkCredential("Username", "Password");
    
            requestStream = HttpWReq.GetRequestStream();
            //Write the data from the source
            requestStream.Write(bytes, 0, bytes.Length);
    
            //get the response
            HttpWRes = (HttpWebResponse)HttpWReq.GetResponse();
    
            var pushStatus = HttpWRes.Headers["X-RIM-Push-Status"];
    
            //if the MDS received the push parameters correctly it will either respond with okay or accepted
            if (HttpWRes.StatusCode == HttpStatusCode.OK || HttpWRes.StatusCode == HttpStatusCode.Accepted)
            {
                success = true;
            }
            else
            {
                success = false;
            }
            //Close the streams
    
            HttpWRes.Close();
            requestStream.Close();
        }
        catch (System.Exception)
        {
            success = false;
        }
    
        return success; 
    }
    

0 个答案:

没有答案