IIS/.NET 上的黑莓推送服务启动器

发布于 2024-09-26 07:25:59 字数 88 浏览 6 评论 0原文

我需要从 ASP.NET 应用程序向 Blackberry 设备发送推送通知。最好的方法是什么?有 .NET 库吗?我是否需要自己编译 PAP 请求或类似的内容?

I need to send push notifications to a Blackberry device from my ASP.NET application. What is the best way to do it? Are there any .NET libraries? Do I need to compile a PAP request by myself or something like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疏忽 2024-10-03 07:26:00

现在有一个开源 C# BlackBerry Push SDK,可供 ASP.NET 应用程序使用和使用。

该项目托管在 CodePlex 此处上。

There is now an open-source C# BlackBerry Push SDK that can be used and consumed by an ASP.NET application.

The project is hosted on CodePlex here.

赢得她心 2024-10-03 07:26:00

如果您不想实现第三方库,那么这段代码对我来说效果很好:

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

If you don't want to implement a 3rd party library, this code worked quite well for me:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文