2010-10-07 46 views
4

我需要從我的ASP.NET應用程序向黑莓設備發送推送通知。什麼是最好的方式來做到這一點?有沒有.NET庫?我是否需要自己編譯一個PAP請求或類似的東西?IIS/.NET上的黑莓推送服務啓動器

回答

1

現在有一個開源的C#BlackBerry Push SDK可以被ASP.NET應用程序使用和使用。

該項目託管在CodePlex here上。

0

如果您不希望實現第三方庫,這個代碼工作得很好我:

private static string UrlForPush = "https://cp815.pushapi.na.blackberry.com"; 
private static string ApplicationId = "your_appid"; 
private static string Password = "your_password"; 

// NOTE: deviceToken below is the BB PIN unique to each device. 

public static string SendPushNotification(string deviceToken, string message) 
{ 
    try 
    { 
     var myPushId = DateTime.Now.ToFileTime().ToString(); 
     var deliverBefore = DateTime.UtcNow.AddMinutes(5).ToString("s", CultureInfo.InvariantCulture) + "Z"; 
     var boundary = "CiTySoUrCeDbLaCkBeRrY"; 
     var url = UrlForPush + "/mss/PD_pushRequest"; 

     var 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\" \"http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd\">"); 
     data.AppendLine("<pap>"); 
     data.AppendLine("<push-message push-id=\"" + myPushId + "\" deliver-before-timestamp=\"" + deliverBefore + "\" source-reference=\"" + ApplicationId + "\">"); 
     data.AppendLine("<address address-value=\"" + deviceToken + "\"/>"); 
     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: " + myPushId); 
     data.AppendLine(""); 
     data.AppendLine(message); 
     data.AppendLine("--" + boundary + "--"); 
     data.AppendLine(""); 

     // Create the Request... 
     var request = (HttpWebRequest)WebRequest.Create(url); 
     request.Method = ("POST"); 
     request.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"; 
     request.Credentials = new NetworkCredential(ApplicationId, Password); 
     request.PreAuthenticate = true; 
     request.ContentType = "multipart/related; boundary=" + boundary + "; type=application/xml"; 
     SetBasicAuthHeader(request, ApplicationId, Password); 
     var bytes = new ASCIIEncoding().GetBytes(data.ToString()); 
     request.ContentLength = bytes.Length; 

     // Write Stream... 
     using (var stream = request.GetRequestStream()) 
     { 
      stream.Write(bytes, 0, bytes.Length); 
     } 

     // Get Response ... 
     var responseAsString = string.Format("Push ID: {0}", myPushId) + Environment.NewLine + Environment.NewLine; 
     using (var response = request.GetResponse() as HttpWebResponse) 
     { 
      var reader = new StreamReader(response.GetResponseStream()); 
      responseAsString += reader.ReadToEnd(); 
      reader.Close(); 
     } 

     // Return... 
     return responseAsString; 
    } 
    catch (Exception ex) 
    { 
     return ex.Message; 
    } 
} 
private static void SetBasicAuthHeader(WebRequest webRequest, String userName, String password) 
{ 
    var authInfo = userName + ":" + password; 
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)); 
    webRequest.Headers["Authorization"] = "Basic " + authInfo; 
}