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 Developer
jeyaprakash
Posts: 33
Registered: ‎07-16-2008
My Device: bold

Re: Push API sample code needed

hi all

 

         Where to find the client side example for push ? , i need to implement PUSH client in BIS devices any suggestions please..

Please use plain text.
Developer
Bullety
Posts: 138
Registered: ‎08-02-2009
My Device: Z10

Re: Push API sample code needed

Dear,

 

@Idonen:

Thank you very much for this code - looks very similar to mine but I've had had a problem in my headers.

 

But together we done it:

 

simply extend your class with this two methods:

 

 

    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("PROXY USERNAME", "PROXY PW");

req.Proxy = proxy;
}

 

The SetProxy is not necessary but I use it to debug my Server application local here on my machine instead of on the server directly.

 

And now comes the working Code:

 

        HttpWebResponse HttpWRes = null;
HttpWebRequest HttpWReq = null;

string pin = "push_all"; // or actual pin of device
string applicationID = "xxx-xxxxxxxxxxxxxxxxx";
string BOUNDARY = "mPsbVQo0a68eIL3OAxnm";
string msg = "testing"; // the message to send

string userName = "xxx-xxxxxxxxxxxxxxxx";
string userPW = "xxxxxxxx";

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();
}
catch (Exception ex)
{
//push failed
}

if (HttpWRes != null)
{
HttpWRes.Close();
}

 Result in HttpWRes:

StatusCode = OK :smileyhappy:

 

Don't ask me how long I needed to come to this simple authentication answer - this was mostly the part who needed the most time in this method :smileywink:

 

 

 

I hope this helpes you and it works for you also :smileywink:

 

 

 

 

@ jeyaprakash:

Install the Push SDK and then look at C:\Program Files (x86)\BPSS\pushsdk (or whereever you installed it).

There will be a sample-client-app which is a client sample.

Please use plain text.
Developer
Bullety
Posts: 138
Registered: ‎08-02-2009
My Device: Z10

Re: Push API sample code needed

To extend:

The line

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

 

 

is not necessary and can be deleted.

 

Best regards,

Bullety

Please use plain text.
Developer
idonen
Posts: 43
Registered: ‎10-03-2008
My Device: Not Specified

Re: Push API sample code needed

Thx bullety,

 

I actually just came to the same answer myself, in that one cannot use the built-in WebCredentials but must set that header manually.

 

The other thing I've discovered is that you need to set the deliver-before-timestamp, like this:

 

 

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

dataToSend.AppendLine("<push-message push-id=\"" + myPushId + "\" deliver-before-timestamp=\"" + deliverBefore + "\" source-reference=\"" + appID + "\">");

 

 

Please use plain text.
Developer
Bullety
Posts: 138
Registered: ‎08-02-2009
My Device: Z10

Re: Push API sample code needed

Thank you.

 

I am currently unfortunately not yet able to push this test if it works really because my client app's registering isn't working yet.

 

But I am sure if the connection to the BPAS Server works correctly (as I expect with the server answer "OK"), the push will be sent correcty.

Please use plain text.
Developer
Bullety
Posts: 138
Registered: ‎08-02-2009
My Device: Z10

Re: Push API sample code needed

Finally I got my registration working and I can report the C# Code works pretty good!

Please use plain text.
New Contributor
hp96cc
Posts: 2
Registered: ‎03-28-2010
My Device: Storm

Re: Push API sample code needed

Hi,

 

Thanks for all the help on this one guys. Just wondering whether any of have experienced this:

 

1. I can only operate the test system using HTTP, not HTTPS for registration

2. If I try and use https to register, I get back error 10001, which relates to an PIN error

 

Has anyone experienced this behavior? If so, do you know how to fix it, I'm not massively keen sending around dun encrypted messages.

 

Cheers,

 

Craig

 

 

Please use plain text.
Contributor
frankwu
Posts: 11
Registered: ‎04-27-2010
My Device: RIM 8310

Re: Push API sample code needed

If you use https, you should get a certificate and private key to do your connection to the Gateway Proxy.

Tried to ask RIM how to get certificate and private key for its Gateway Proxy.

 

BTW, do you have java code for the Push Request?

 

thanks

Please use plain text.
Regular Contributor
mdicesare
Posts: 78
Registered: ‎04-28-2010
My Device: BlackBerry 10 Dev Alpha

Re: Push API sample code needed

As far as I know you need to use HTTP when registering with RIM BIS Push Service

Please use plain text.
Developer
Bullety
Posts: 138
Registered: ‎08-02-2009
My Device: Z10

Re: Push API sample code needed

Dear hp96cc,

 

If you only want your data be encrypted, why don't you encrypt your data and move it into the pap xml?

 

On the device, you could decrypt it again.

 

If you only encrypt the connection between your server and the BB servers, what about the rest of the transaction?

 

Possibly the BB server or any other server in the transaction line "could" get hacked and your data would be there in plain text...?!

 

Or did I not understand you correctly?

 

 

Best regards,

Bullety

Please use plain text.