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
Contributor
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

BlackBerry C# push message

I am working with BlackBerry Push Service. I have successfully registered for push service and tested it on my device by writing a simple php script thats sends a message to the BlackBerry device. I am working on c# server side code to send a push message to the device.  I am not sure whether I am entering the correct credentials. I am struggling with this for a very long time now. Your help is much needed. Do I need to perform any other task before running the above c# code on the server? Kindly help.

 

Following is my code for server side:

 

string userName = "<app ID>";
string userPW = "<password>";
public static void SetBasicAuthHeader(WebRequest req, String userName, String userPassword)
{
string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Headers["Authorization"] = "Basic " + authInfo;
}
public static void SetProxy(WebRequest req)
{
Uri newUri = new Uri("PROXYURL");
WebProxy proxy = new WebProxy();
proxy.Address = newUri;
proxy.Credentials = new NetworkCredential("<portalUsername", "<portalPassword");
req.Proxy = proxy;
}
public bool pushTest(string msg)
{
HttpWebResponse HttpWRes = null;
HttpWebRequest HttpWReq = null;
string pin = "<BBpin>"; // or actual pin of device
string applicationID = "<appID>";
string BOUNDARY = "Boundary";
string msg1 = "testing c#"; // the message to send
string userName = "appID";
string userPW = "password";
string url = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest";
HttpWReq = (HttpWebRequest)WebRequest.Create(url);
//SetProxy(HttpWReq);
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";
SetBasicAuthHeader(HttpWReq, userName, userPW);
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();
// MessageBox.show(""+HttpWRes);

}
catch (Exception ex)
{
//push failed
}
if (HttpWRes != null)
{
HttpWRes.Close();
}
return true;
}
Developer
Posts: 230
Registered: ‎01-12-2013
My Device: z30
My Carrier: Mobile Vikings

Re: BlackBerry C# push message

[ Edited ]

I think you should set the deliver-before-timestamp property as well. What is the server's response?

 

This is how my message looks like. If I forget the deliver-before-timestamp, it will not work.

 

--PMasdfglkjhqwert
Content-Type: application/xml; charset=UTF-8
<?xml version="1.0"?> <!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd"> <pap> <push-message
push-id="push.example.com@1369139017071"
source-reference="4895-45668ede789MM608a51m02ic985652q85r"
deliver-before-timestamp="2013-05-21T14:27:48Z">
<address address-value="PIN" />
<quality-of-service delivery-method="unconfirmed" /> </push-message> </pap> --PMasdfglkjhqwert Content-Encoding: binary Content-Type: text/html Push-Message-ID: push.example.com@1369139017071
This is the message
--PMasdfglkjhqwert--

 

______________________________________________________
BB10-OAuth: GitHub
Contributor
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: BlackBerry C# push message

Thank you for replying. I have tested with this line of code as well. I get the returned response "OK" but the device never receives the message. Do you have working code for this? Can you please explain. Are the credentials being used above in the code posted correct? Please advice.

Retired
Posts: 311
Registered: ‎08-08-2012
My Device: BlackBerry Z10
My Carrier: Rogers

Re: BlackBerry C# push message

What's the actual response you are getting after sending a push?

Can you show what you get back.

 

Thanks,

 

Matt

Contributor
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: BlackBerry C# push message

[ Edited ]

The code posted above is throwing an error:

 

Push failed! System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 68.171.224.60:80
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
   at System.Net.HttpWebRequest.GetRequestStream()

 I tried another method which returns the status code "OK" but again no message is received the server side. My code is as below:

 

private void Pushtest()
    {
        HttpWebResponse HttpWRes = null;
        HttpWebRequest HttpWReq = null;
        string pin = "xxxxxxxx"; // or actual pin of device
        string applicationID = "xxxx-xxxxxxxxxxxxxx";
        string BOUNDARY = "mPsbVQo0a68eIL3OAxnm";
        string msg = "testing c#"; // the message to send

        string userName = "xxxx-xxxxxxxxxxxxxx";
        string userPW = "xxxxxxxxxxx";

        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);
        string rawCredentials = string.Format("{0}:{1}", userName, userPW);
        HttpWReq.Headers.Add("Authorization",
            string.Format(
                "Basic {0}",
                Convert.ToBase64String(Encoding.UTF8.GetBytes(rawCredentials))));

        HttpWReq.PreAuthenticate = true;
        HttpWReq.ContentType = "multipart/related; boundary=" + BOUNDARY + "; type=application/xml";
        string DeliverBefore = DateTime.UtcNow.AddMinutes(5).ToString("s", System.Globalization.CultureInfo.InvariantCulture) + "Z";
        Response.Write(DeliverBefore);
        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();
        }
    }

 In order to read the response I added the following line of code:

try
        {
            HttpWRes = (HttpWebResponse)HttpWReq.GetResponse();
            Stream Answer = HttpWRes.GetResponseStream();
        }
        catch (Exception ex)
        {
            //push failed
        }
but the result for Answer is:

Message = "This stream does not support seek operations."

Length = 'requestStream.Length' threw an exception of type 'System.NotSupportedException'

 

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: BlackBerry C# push message

FYI this is also being discussed in this thread:

http://supportforums.blackberry.com/t5/BlackBerry-Push-Development/How-Send-Push-Notifcation-Using-C...

Garett
@garettBeuk
--
Goodbye everybody!
Contributor
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: BlackBerry C# push message

And I am the one doing it in both of these threads..
Retired
Posts: 311
Registered: ‎08-08-2012
My Device: BlackBerry Z10
My Carrier: Rogers

Re: BlackBerry C# push message

Maybe just stick to this one to avoid confusion. Smiley Happy

Contributor
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: BlackBerry C# push message

Sure. Since I am struggling with BlackBerry Push Service for a very long time now, can you answer the following; 

 

I have server side code in asp.net to send the push message. My device is on BIS and I have no BES installed. I am running the server side code from my local machine. Does my server need any kind of registration to be able to push message. My device is successfully registered to receive Push messages and this has been tested by sending message from a simple php file running on my server. However, the asp.net code fails to deliver the messages.

 

Please help.