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

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

Thank you for the reply. I will work on the .net issue. But my question at the start was "how to send a push message to BlackBerry device using c# code". In this case, while I correct my c# code, I would need help how to write with BlackBerry Push Service. Considering I am a beginner, how should I start? Can BlackBerry guide me in the right direction so I can write a method in c#. Please guide in this matter as well.

Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

Push is just sending an HTTP POST, which it looks like your code is doing. HTTP POST is not specific to BlackBerry.

 

Once you determine the source of the issue with your code we should be able to see what is coming back from the Push Service. Depending on the result we will know how to proceed.

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

Can you guide what should be the http URL to post? I am not working on BES. What credentials need to be passed along?

Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

You would use the URL and credentials received when you applied for a push account here:

https://www.blackberry.com/profile/?eventId=8121

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

Hi

 

I worked around to get the server response. I updated my method as below:

 

private void pushMessageSample(string pushedMessage)
    {
        String appid="xxxx-xxxxxxxxxxxxxxxxxxxxxx";
        String password = "xxxxxxxxx";
        String deliverbefore = DateTime.UtcNow.AddMinutes(5).ToString("s",System.Globalization.CultureInfo.InvariantCulture) + "Z";
        String pushPin = "xxxxxxx";
        String Boundary = "mPsbVQo0a68eIL3OAxnm";

 String messageid = DateTime.UtcNow.AddMinutes(5).ToString("s",System.Globalization.CultureInfo.InvariantCulture) + "Z";

StringBuilder 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\">");
            Data.AppendLine("<pap>");
            Data.AppendLine("<push-message push-id=" + (char)34 + messageid + (char)34 + " deliver-before-timestamp=" + 
(char)34 + deliverbefore + (char)34 + " source-reference=" + (char)34 + appid + (char)34 + ">");
            Data.AppendLine("<address address-value=\"" + pushPin + "\"/>");
            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: " + messageid);
            Data.AppendLine("");
            Data.AppendLine(pushedMessage);
            Data.AppendLine("--" + Boundary + "--");
            Data.AppendLine("");

        byte[] bytes = Encoding.ASCII.GetBytes(Data.ToString());
        String BESName = "cp3934.pushapi.eval.blackberry.com/mss/PD_pushRequest";
        String httpURL = "https://" + BESName + "/push?DESTINATION=288CA325&PORT=33672&REQUESTURI=/";

            WebRequest tRequest;
            tRequest = WebRequest.Create(httpURL);
            tRequest.Method = "post";
            tRequest.ContentType = "text/plain";

            tRequest.Headers.Add("X-Rim-Push-Id", pushPin + "~" + DateTime.Now); //"~" +pushedMessage +
            tRequest.Headers.Add("X-Rim-Push-Reliability", "application-preferred");
            tRequest.ContentLength = bytes.Length;
            tRequest.Credentials = new NetworkCredential(appid, password); 

            Stream dataStream = tRequest.GetRequestStream();
            dataStream.Write(bytes, 0, bytes.Length);
            dataStream.Close();

            WebResponse tResponse = tRequest.GetResponse();

            dataStream = tResponse.GetResponseStream();

            StreamReader tReader = new StreamReader(dataStream);

            String sResponseFromServer = tReader.ReadToEnd();

            tReader.Close();
            dataStream.Close();
            tResponse.Close();
    }

 I am now able to capture the server response as you adviced. My server response is as below:

 

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<title>Pushdata Service</title>
	<body bgcolor="white" leftmargin="0" marginheight="0" marginwidth="0" topmargin="0">
		<form enctype="application/x-www-form-urlencoded" method="get">

			<!-- Header -->

			<!-- static error header library -->
			
			
			

			<br clear="none"/>

			<!-- Errorcode -->

			<!-- dynamic @[errorcode] token -->
			
			<font color="#777777" size="1" style="verdana">Error </font>
			<br clear="none"/>
			<br clear="none"/>
			


			<!-- Errorbody -->

			<!-- dynamic @[errorbody] token -->
			
			<font color="#777777" size="1" style="verdana">The content you have requested is not available</font>
			<br clear="none"/>
			

			<!-- Error message -->

			<!-- dynamic @[errormsg] token -->
			
			<br clear="none"/>
			<font color="#777777" size="1" style="verdana">push command is not currently in service!</font>
			<br clear="none"/>
			<br clear="none"/>
			

			<!-- Footer -->

			<!-- static error footer library -->
			
			
			

			<br clear="none"/>
		</form>
	</body>
</html>

 So in there it says "Push command not currently in service". Can you guide from here? Why is the device unable to receive the message? The php server side code delivers the message successfully. Am I missing anything here? Please help!!

Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

Ok, well at least we're getting a response now.

 

The issue with your coe is that you are pushing using the BES technique but you are pushing to BIS.

 

Your URL should just be:

https://cpXXXX.pushapi.eval.blackberry.com/mss/PD_pushRequest (where 'xxxx' is your ID)

 

And you dont need these headers:

tRequest.Headers.Add("X-Rim-Push-Id", pushPin + "~" + DateTime.Now); //"~" +pushedMessage +
            tRequest.Headers.Add("X-Rim-Push-Reliability", "application-preferred");
Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

Thank you for replying. I updated the code as you suggested. I am now connecting with the following URL:

 

byte[] bytes = Encoding.ASCII.GetBytes(Data.ToString());
        String httpURL = "https://cp3934.pushapi.eval.blackberry.com/mss/PD_pushRequest";

            WebRequest tRequest;
            tRequest = WebRequest.Create(httpURL);
            tRequest.Method = "post";
            tRequest.ContentType = "text/plain";

            tRequest.ContentLength = bytes.Length;
            tRequest.Credentials = new NetworkCredential(appid, password); 

 It now returns me a WebException: The remote server returned an error: (401) Unauthorized. My username is the App ID and the password as provided by BlackBerry Push Service.

Please use plain text.
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

I made progress, message does not deliver, but I now get the following response:

 

<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd"><pap><badmessage-response code="2000" desc="2000" bad-message-fragment="2000"/></pap>

 I added th following code to counter the authorization issue:

 

tRequest.Credentials = new NetworkCredential(appid, password);

            tRequest.PreAuthenticate = true;
            tRequest.ContentType = "multipart/related; boundary=" + Boundary + "; type=application/xml";

            string rawCredentials = string.Format("{0}:{1}", appid, password);
            tRequest.Headers.Add("Authorization",
                string.Format(
                    "Basic {0}",
                    Convert.ToBase64String(Encoding.UTF8.GetBytes(rawCredentials))));

            SetBasicAuthHeader(tRequest, appid, 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;
    }

 

Please use plain text.
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

My issue has been resolved. I want to thank for directing me in the right direction. Much appreciated. I would like to share my code for anyone else struggling with this. Thanks again!

private void pushMessageSample(string pushedMessage)
    {
        String appid="xxxx-xxxxxxxxxxxxxxxxxxxxxxxx";
        String password = "xxxxxxxx";
        String deliverbefore = DateTime.UtcNow.AddMinutes(5).ToString("s",System.Globalization.CultureInfo.InvariantCulture) + "Z";
        String pushPin = "xxxxxxxxx";
        String Boundary = "mPsbVQo0a68eIL3OAxnm";

 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=" + (char)34 + myPushId + (char)34 + " deliver-before-timestamp=" +
(char)34 + deliverbefore + (char)34 + " source-reference=" + (char)34 + appid + (char)34 + ">");
 //dataToSend.AppendLine("<push-message push-id=\"" + myPushId + "\" source-reference=\"" + appid + "\">");
 dataToSend.AppendLine("<address address-value=\"" + pushPin + "\"/>");
 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(pushedMessage);

 dataToSend.AppendLine("--" + Boundary + "--");
 dataToSend.AppendLine("");
 
 byte[] bytes = Encoding.ASCII.GetBytes(dataToSend.ToString());
        String httpURL = "https://cpxxxx.pushapi.eval.blackberry.com/mss/PD_pushRequest";

            WebRequest tRequest;
            tRequest = WebRequest.Create(httpURL);
            //SetProxy(tRequest);
            tRequest.Method = "POST";
            //tRequest.ContentType = "text/plain";

            //tRequest.ContentLength = bytes.Length;
            tRequest.Credentials = new NetworkCredential(appid, password);

            tRequest.PreAuthenticate = true;
            tRequest.ContentType = "multipart/related; boundary=" + Boundary + "; type=application/xml";
            tRequest.ContentLength = bytes.Length;
            string rawCredentials = string.Format("{0}:{1}", appid, password);
            tRequest.Headers.Add("Authorization",
                string.Format(
                    "Basic {0}",
                    Convert.ToBase64String(Encoding.UTF8.GetBytes(rawCredentials))));

            SetBasicAuthHeader(tRequest, appid, password);

            Stream dataStream = tRequest.GetRequestStream();
            dataStream.Write(bytes, 0, bytes.Length);
            dataStream.Close();

            WebResponse tResponse = tRequest.GetResponse();

            dataStream = tResponse.GetResponseStream();

            StreamReader tReader = new StreamReader(dataStream);

            String sResponseFromServer = tReader.ReadToEnd();

            tReader.Close();
            dataStream.Close();
            tResponse.Close();
    }

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

 

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

This thread helped me out, and the key seems to be that my Content-Type wasn't correct.

I was using "multipart/related; boundary=BOUNDARYTEXT"

With just that the server was complaining that the "Received content type is not 'application/xml' ".

Changing it to "multipart/related; boundary=BOUNDARYTEXT; type=application/xml" as shown in the above code solved that issue.

Thanks!

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.