2013-03-16 62 views
1
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Text; 
using System.Net; 
using System.IO; 

namespace PushSharp.Blackberry 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

      pushToWidget("Hi Anoop If You will Get Notifcation please miss call me 8130513899"); 

     } 



     public bool pushToWidget(string pushedMessage) 
     { 
      // String BESAddress = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest"; 
      //String BESWebserverListenPort = "pushPort"; 
      String widgetNotificationUrl = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest"; 
      String pushUserName = "3457- 
B730k4394m49rOr96r33M8t74303c51k728"; 
      String pushPassword = "smnBIWO3"; 


      // String pushPort = "33215"; 
      string Boundary = "Boundary "; 
      string DeliverBefore = DateTime.UtcNow.AddMinutes(5).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=\"" + "push_all" + "\"/>"); 
      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 
      { 
       //http://<BESName>:<BESPort>/push?DESTINATTION=<PIN/EMAIL>&PORT=<PushPort>&REQUESTURI=/ 
       // Build the URL to define our connection to the BES. 

       String BESName = "cp3457.pushapi.eval.blackberry.com/mss/PD_pushRequest"; 

       string httpURL = "https://" + BESName + "/push?DESTINATION=2B838E45&PORT=33215&REQUESTURI=/"; 

      // 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", "push_all" + "~" + DateTime.Now); //"~" +pushedMessage + 
       HttpWReq.Headers.Add("X-Rim-Push-Reliability", "application-preferred"); 
       HttpWReq.Headers.Add("X-Rim-Push-NotifyURL", (widgetNotificationUrl + "push_all" + "~" + pushedMessage + "~" + DateTime.Now).Replace(" ", "")); 

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

       // HttpWReq.Credentials = new MDSCredentials(pushUserName, pushPassword); 
       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(); 

       //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; 
     } 


    } 
} 

請檢查該代碼,代碼運行良好並獲得響應成功,但是當我在PHP中使用此代碼無法使用.NET

Blackberry Push notification using C# as server side 無法發送它工作正常發送推送Notifcation在黑莓並得到通知我不知道我在哪裏做錯

+0

這可能會有所幫助http://pastebin.com/SHqrGdbF – Signare 2013-03-19 11:46:02

+0

我處於完全相同的情況;獲得響應代碼「成功」,但該設備永遠不會收到任何東西。我可以確認該設備具有活動的BIS,並且該設備已成功「註冊」爲Push消息。誰能幫忙? – Sarah 2013-03-20 07:59:50

回答

0

我設法解決這個問題。代碼中的主要問題是您用於發佈Push Message的URL。據我所知,你實際上是在你的設備中使用BIS來接收信息而不是BES。因此,您正在使用的httpURL可以編輯爲String httpURL = "https://cpxxxx.pushapi.eval.blackberry.com/mss/PD_pushRequest";。的完整的工作代碼是如下:

private void pushMessageSample(string pushedMessage) 
{ 
String appid="xxxx-xxxxxxxxxxxxxxxxxxxxxxxxxx"; 
String password = "xxxxxx"; 
String deliverbefore = DateTime.UtcNow.AddMinutes(5).ToString("s",System.Globalization.CultureInfo.InvariantCulture) + "Z"; 
String pushPin = "xxxxxxxx"; 
String Boundary = "mPsbVQo0a68eIL3OAxnm"; 

StringBuilder dataToSend = new StringBuilder(); 

dataToSend.AppendLine("--" + Boundary); 
dataToSend.AppendLine("Content-Type: application/xml; charset=UTF-8"); 
dataToSend.AppendLine(""); 
dataToSend.AppendLine("<?xml version=\"1.0\"?>"); 
dataToSend.AppendLine("<!DOCTYPE pap PUBLIC \"-//WAPFORUM//DTD PAP 2.1//EN\" \"http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd\">"); 
dataToSend.AppendLine("<pap>"); 
string myPushId = DateTime.Now.ToFileTime().ToString(); 
dataToSend.AppendLine("<push-message push-id=" + (char)34 + myPushId + (char)34 + " deliver-before-timestamp=" + 
(char)34 + deliverbefore + (char)34 + " source-reference=" + (char)34 + appid + (char)34 + ">"); 
//dataToSend.AppendLine("<push-message push-id=\"" + myPushId + "\" source-reference=\"" + appid + "\">"); 
dataToSend.AppendLine("<address address-value=\"" + pushPin + "\"/>"); 
dataToSend.AppendLine("<quality-of-service delivery-method=\"unconfirmed\"/>"); 
dataToSend.AppendLine("</push-message>"); 
dataToSend.AppendLine("</pap>"); 
dataToSend.AppendLine("--" + Boundary); 
dataToSend.AppendLine("Content-Type: text/plain"); 
dataToSend.AppendLine("Push-Message-ID: " + myPushId); 
dataToSend.AppendLine(""); 
dataToSend.AppendLine(pushedMessage); 
dataToSend.AppendLine("--" + Boundary + "--"); 
dataToSend.AppendLine(""); 

byte[] bytes = Encoding.ASCII.GetBytes(dataToSend.ToString()); 
String httpURL = "https://cpxxxx.pushapi.eval.blackberry.com/mss/PD_pushRequest"; 

WebRequest tRequest; 
tRequest = WebRequest.Create(httpURL); 
tRequest.Method = "POST"; 
tRequest.Credentials = new NetworkCredential(appid, password); 
tRequest.PreAuthenticate = true; 
tRequest.ContentType = "multipart/related; boundary=" + Boundary + "; type=application/xml"; 
tRequest.ContentLength = bytes.Length; 
string rawCredentials = string.Format("{0}:{1}", appid, password); 
tRequest.Headers.Add("Authorization",string.Format("Basic {0}", 
Convert.ToBase64String(Encoding.UTF8.GetBytes(rawCredentials)))); 
SetBasicAuthHeader(tRequest, appid, password); 

Stream dataStream = tRequest.GetRequestStream(); 
dataStream.Write(bytes, 0, bytes.Length); 
dataStream.Close(); 

WebResponse tResponse = tRequest.GetResponse(); 
dataStream = tResponse.GetResponseStream(); 
StreamReader tReader = new StreamReader(dataStream); 
String sResponseFromServer = tReader.ReadToEnd(); 

tReader.Close(); 
dataStream.Close(); 
tResponse.Close(); 
} 

public static void SetBasicAuthHeader(WebRequest req, String appID, String userPassword) 
{ 
string authInfo = appID + ":" + userPassword; 
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); 
req.Headers["Authorization"] = "Basic " + authInfo; 
} 

方法SetBasicAuthHeader是驗證用戶訪問用戶做出POST請求。