黑莓推送消息

时间:2013-02-18 12:19:35

标签: c# .net blackberry push-notification push

我已经构建了一个简单的BlackBerry App,现在我想在其中实现一个小的聊天功能。想法是通过Web服务向服务器发送消息。这已成功实施。我现在需要在BlackBerry App中接收回复。服务器的响应已成功捕获。如何将此响应推送至BlackBerry手机。我已通过电子邮件中收到的评估凭证在BlackBerry中成功注册了推送服务。我在C#中编写了一个简单的服务器端代码,但它从未起作用。下面是我的C#代码。任何人都可以指导我吗?我的设备从未收到过推送消息。可能是我做错了。

请帮助!

public bool pushToWidget(string pushedMessage, string pushPin)
    {
        String BESAddress = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest";
        String BESWebserverListenPort = "106"; //random
        String widgetNotificationUrl = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest";
        String pushUserName = "xxx";
        String pushPassword = "xxx";
        String pushPort = "xxxx ";
        string Boundary = "Boundary ";
        String ID = "xxx-xxxx";
        string DeliverBefore = DateTime.UtcNow.AddMinutes(60).ToString("s", 

        System.Globalization.CultureInfo.InvariantCulture) + "Z";
        Response.Write(DeliverBefore);
        bool success = true;
        StringBuilder Data = new StringBuilder();
        Data.AppendLine("--" + Boundary);
        Data.AppendLine("Content-Type: application/xml; charset=utf-8");
        Data.AppendLine("");
        Data.AppendLine("<?xml version=\"1.0\"?>");
        Data.AppendLine("<!DOCTYPE pap PUBLIC \"-//WAPFORUM//DTD PAP 2.1//EN\">");
        Data.AppendLine("<pap>");
        Data.AppendLine("<push-message push-id=" + (char)34 + ID + (char)34 + 
           " deliver-before-timestamp=" + (char)34 + DeliverBefore + (char)34 + 
           " source-reference=" + (char)34 + pushUserName + (char)34 + ">");
        Data.AppendLine("<address address-value=\"" + pushPin + "\"/>");
        Data.AppendLine("<quality-of-service delivery-method=\"unconfirmed\"/>");
        Data.AppendLine("</push-message>");
        Data.AppendLine("</pap>");
        Data.AppendLine("--" + Boundary);
        Data.AppendLine("Content-Type: text/plain");
        Data.AppendLine("Push-Message-ID: " + ID);
        Data.AppendLine("");
        Data.AppendLine(pushedMessage);
        Data.AppendLine("--" + Boundary + "--");
        Data.AppendLine("");
        byte[] bytes = Encoding.ASCII.GetBytes(Data.ToString());

        Stream requestStream = null;
        HttpWebResponse HttpWRes = null;
        HttpWebRequest HttpWReq = null;

        try
        { 
            string httpURL = BESAddress + ":" + BESWebserverListenPort
                + "/push?DESTINATION=" + pushPin + "&PORT=" + pushPort
                + "&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", pushPin + "~" + DateTime.Now); //"~" + pushedMessage +
            // HttpWReq.Headers.Add("X-Rim-Push-Reliability", "application-preferred");
            HttpWReq.Headers.Add("X-Rim-Push-NotifyURL", (widgetNotificationUrl + 
                pushPin + "~" + pushedMessage + "~" + DateTime.Now).Replace(" ", ""));

            // *************************

            HttpWReq.Credentials = new NetworkCredential(pushUserName, pushPassword);
            Console.WriteLine(pushedMessage);
            requestStream = HttpWReq.GetRequestStream();
            //Write the data from the source
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            //get the response
            HttpWRes = (HttpWebResponse)HttpWReq.GetResponse();
            String response = HttpWRes.ToString();

            //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 e)
        {
            success = false;
        }

        return success;
    }

0 个答案:

没有答案