Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

BlackBerry Push Development

Reply
New Member
Posts: 1
Registered: ‎10-10-2011
My Device: Bold 9700
My Carrier: MTN

.net or C# Push Initiator code example

I am trying to push messages to my device from a .net appliacation. the app can be windows app or a web app.

I have got as far as below by searching the web but when I run it, it gives me the error, "The remote server returned an error: (401) Unauthorized."

 

I have set up the sample Push Initiator by following the documentation I got off the blackberry web site and I do receive pushes on my device from it so I know my credentials are correct and are still valid but I would like to have a C# app doing the pushes because it will be easier for me to maintain and monitor. Does anyone have an example that is currently working or tell me why I am getting the error?

 

Thanks.

 

Below is the code I have a the moment

        

private void Pushtest()

        {

            HttpWebResponse HttpWRes = null;

            HttpWebRequest HttpWReq = null;

 

            string pin = "ABCDEF12"; // or actual pin of device

            string applicationID = "xxx-xxxxxxxxxxxxxxxxxx";

            string BOUNDARY = "mPsbVQo0a68eIL3OAxnm";

            string msg = "testing"; // the message to send

 

            string userName = "xxx-xxxxxxxxxxxxxxxxxx";

            string userPW = "xxxxxxxx";

 

            string url = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest";

 

            HttpWReq = (HttpWebRequest)WebRequest.Create(url);

            HttpWReq.Method = ("POST");

            HttpWReq.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";

 

            HttpWReq.Credentials = new NetworkCredential(userName, userPW);

 

            HttpWReq.PreAuthenticate = true;

            HttpWReq.ContentType = "multipart/related; boundary=" + BOUNDARY + "; type=application/xml";

 

            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=\"" + myPushId + "\" source-reference=\"" + applicationID + "\">");

            dataToSend.AppendLine("<address address-value=\"" + pin + "\"/>");

            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(msg);

 

            dataToSend.AppendLine("--" + BOUNDARY + "--");

            dataToSend.AppendLine("");

 

            Stream requestStream = null;

            string pushResult = "";

            try

            {

                requestStream = HttpWReq.GetRequestStream();

            }

            catch (Exception ex)

            {

                pushResult = "Push failed! " + ex.ToString();

            }

            byte[] outStr = new ASCIIEncoding().GetBytes(dataToSend.ToString());

            requestStream.Write(outStr, 0, outStr.Length);

            requestStream.Close();

 

            try

            {

                HttpWRes = (HttpWebResponse)HttpWReq.GetResponse();

            }

            catch (Exception ex)

            {

                //push failed

            }

 

            if (HttpWRes != null)

            {

                HttpWRes.Close();

            }

}

New Developer
Posts: 7
Registered: ‎04-06-2012
My Device: Bold 9900

Re: .net or C# Push Initiator code example

I have found that using NetworkCredentials simply doesn't work - I'm not sure why...

In any case you can use the following code to manually setup the authorization header:

 

string userName = "RIM app id";
string password = "RIM password";
string rawCredentials = string.Format("{0}:{1}", userName, password);
request.Headers.Add("Authorization",
	string.Format(
		"Basic {0}",
		Convert.ToBase64String(Encoding.UTF8.GetBytes(rawCredentials))));

 

That works for me (request in this case is an HttpWebRequest object)...

 

Regards,

Demented Devil

If it ain't broke try harder...

My stuff: Bold 9900/PlayBook 64Gb/BB10 Dev Alpha B DTAC/Thailand

BESAdmin
WinServer 2008R2 / Exchange 2010
24Gb RAM, > 2TB HARDDISK

Developing for BB10 with Cascades, QML and C++