IIS/.NET上的Blackberry推送服务启动器
我需要从ASP.NET应用程序向Blackberry设备发送推送通知。最好的方法是什么?有任何.NET库吗?我是否需要自己编译PAP请求或类似的东西 现在有了一个开源的C#BlackBerry Push SDK,ASP.NET应用程序可以使用和使用它IIS/.NET上的Blackberry推送服务启动器,.net,asp.net,blackberry,push-notification,blackberry-push,.net,Asp.net,Blackberry,Push Notification,Blackberry Push,我需要从ASP.NET应用程序向Blackberry设备发送推送通知。最好的方法是什么?有任何.NET库吗?我是否需要自己编译PAP请求或类似的东西 现在有了一个开源的C#BlackBerry Push SDK,ASP.NET应用程序可以使用和使用它 该项目托管在CodePlex上。如果您不想实现第三方库,此代码对我来说非常有效: private static string UrlForPush = "https://cp815.pushapi.na.blackberry.com"; priva
该项目托管在CodePlex上。如果您不想实现第三方库,此代码对我来说非常有效:
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;
}
私有静态字符串UrlForPush=”https://cp815.pushapi.na.blackberry.com";
私有静态字符串ApplicationId=“your_appid”;
私有静态字符串Password=“您的密码”;
//注:下面的deviceToken是每个设备的唯一BB引脚。
公共静态字符串SendPushNotification(string deviceToken,字符串消息)
{
尝试
{
var myPushId=DateTime.Now.ToFileTime().ToString();
var deliverBefore=DateTime.UtcNow.AddMinutes(5).ToString(“s”,CultureInfo.InvariantCulture)+“Z”;
var boundary=“城市资源黑莓”;
var url=UrlForPush+“/mss/PD_pushRequest”;
var data=新的StringBuilder();
数据。附录行(“-”+边界);
AppendLine(“内容类型:application/xml;charset=UTF-8”);
数据。附录行(“”);
数据。附录行(“”);
数据。附录行(“”);
数据。附录行(“”);
数据。附录行(“”);
数据。附录行(“”);
数据。附录行(“”);
数据。附录行(“”);
数据。附录行(“”);
数据。附录行(“-”+边界);
data.AppendLine(“内容类型:text/plain”);
data.AppendLine(“推送消息ID:+myPushId”);
数据。附录行(“”);
数据线(报文);
数据。附录行(“-”+边界+“-”);
数据。附录行(“”);
//创建请求。。。
var request=(HttpWebRequest)WebRequest.Create(url);
方法=(“POST”);
request.Accept=“text/html,image/gif,image/jpeg,*;q=.2,*/*;q=.2”;
request.Credentials=新的网络凭据(应用程序ID、密码);
request.PreAuthenticate=true;
request.ContentType=“multipart/related;boundary=“+boundary+”;type=application/xml”;
SetBasicAuthHeader(请求、应用程序ID、密码);
var bytes=new ascienceoding().GetBytes(data.ToString());
request.ContentLength=字节.Length;
//写入流。。。
使用(var stream=request.GetRequestStream())
{
stream.Write(字节,0,字节.长度);
}
//得到回应。。。
var responseAsString=string.Format(“推送ID:{0}”,myPushId)+Environment.NewLine+Environment.NewLine;
使用(var response=request.GetResponse()作为HttpWebResponse)
{
var reader=newstreamreader(response.GetResponseStream());
responseAsString+=reader.ReadToEnd();
reader.Close();
}
//返回。。。
返回响应字符串;
}
捕获(例外情况除外)
{
返回ex.消息;
}
}
私有静态void SetBasicAuthHeader(WebRequest WebRequest、字符串用户名、字符串密码)
{
var authInfo=userName+“:”+密码;
authInfo=Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
webRequest.Headers[“Authorization”]=“Basic”+authInfo;
}