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
Developer
challgren
Posts: 70
Registered: ‎05-04-2009
My Carrier: T-Mobile/Wifi

Re: Push API sample code needed

They are in the PushSDK.properties files. I'm still trying to see if they are even called using the sample-app. So far the above code doesn't work I'm still trying to tweak it

If you find this post useful please give kudo's
Please use plain text.
Developer
challgren
Posts: 70
Registered: ‎05-04-2009
My Carrier: T-Mobile/Wifi

Re: Push API sample code needed

Haha you were right about it being pushaip.eval.blackberry.com I spent like 4 hours trying to use push.eval.blackberry.com

If you find this post useful please give kudo's
Please use plain text.
Developer
Bullety
Posts: 138
Registered: ‎08-02-2009
My Carrier: T-Mobile

Re: Push API sample code needed

Gread, so your push works now?

 

I just want to know if I am on the right path to get it working under C#, too. :smileywink:

Please use plain text.
Developer
challgren
Posts: 70
Registered: ‎05-04-2009
My Carrier: T-Mobile/Wifi

Re: Push API sample code needed

Yep!!! After spending about 30 hours total I was able to get PHP to push a notification. Here is the code that is working and does some random generation because the sample push app on the BB does listen for duplicate messages and ignores them.

 

// create a new cURL resource
$ch = curl_init();
$messageid = microtime();

$data = '--mPsbVQo0a68eIL3OAxnm'. "\r\n" .
'Content-Type: application/xml; charset=UTF-8' . "\r\n\r\n" .
'<?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="' . $messageid . '" deliver-before-timestamp="2010-03-29T16:32:44Z" source-reference="Your APP ID">
<address address-value="PIN, Email, or 'push_all'"/>
<quality-of-service delivery-method="unconfirmed"/>
</push-message>
</pap>' . "\r\n" .
'--mPsbVQo0a68eIL3OAxnm' . "\r\n" .
'Content-Type: text/plain' . "\r\n" .
'Push-Message-ID: ' . $messageid . "\r\n\r\n" .
$_POST['message'] . "\r\n" .
'--mPsbVQo0a68eIL3OAxnm--' . "\n\r";

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://pushapi.eval.blackberry.com/mss/PD_pushRequest");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "BlackBerry Push Service SDK/1.0");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "Your username and password assigned by RIM");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/related; boundary=mPsbVQo0a68eIL3OAxnm; type=application/xml", "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", "Connection: keep-alive"));

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

 

If you find this post useful please give kudo's
Please use plain text.
Developer
Bullety
Posts: 138
Registered: ‎08-02-2009
My Carrier: T-Mobile

Re: Push API sample code needed

[ Edited ]

Congratulations and thanks for the code.

 

How is this set? Which one is it (the username and password for the Portal access or the password for the application)?

I think second, but what's the username then?

curl_setopt($ch, CURLOPT_USERPWD, "Your username and password assigned by RIM");

I am currently working at the subscribtion part (because I think I can only push if my BlackBerry is subscribed to my app) but I have problems with the authentication.

 

 

In your code, I found no port settings - for what will the port of my application assigned by RIM be used?

Or is it the listen port on the client?

Please use plain text.
Developer
challgren
Posts: 70
Registered: ‎05-04-2009
My Carrier: T-Mobile/Wifi

Re: Push API sample code needed

[ Edited ]

The username is the app id and the password is the app password right next to the source IP in the email RIM sent you. The port is stored by RIM so you must use the port that you were assigned. I haven't worked on the subscription aspect yet. But here is some polished code that parses the response from the server

 

// APP ID provided by RIM
$appid = '';
// Password provided by RIM
$password = '';

//Deliver before timestamp
$deliverbefore = gmdate('Y-m-d\TH:i:s\Z', strtotime('+5 minutes'));

//An array of address must be in PIN format or "push_all"
$addresstosendto[] = '';

$addresses = '';
foreach ($addresstosendto as $value) {
    $addresses .= '<address address-value="' . $value . '"/>';
}

// create a new cURL resource
$err = false;
$ch = curl_init();
$messageid = microtime(true);

$data = '--mPsbVQo0a68eIL3OAxnm'. "\r\n" .
'Content-Type: application/xml; charset=UTF-8' . "\r\n\r\n" .
'<?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="' . $messageid . '" deliver-before-timestamp="' . $deliverbefore . '" source-reference="' . $appid . '">'
. $addresses .
'<quality-of-service delivery-method="unconfirmed"/>
</push-message>
</pap>' . "\r\n" .
'--mPsbVQo0a68eIL3OAxnm' . "\r\n" .
'Content-Type: text/plain' . "\r\n" .
'Push-Message-ID: ' . $messageid . "\r\n\r\n" .
stripslashes($_POST['message']) . "\r\n" .
'--mPsbVQo0a68eIL3OAxnm--' . "\n\r";

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://pushapi.eval.blackberry.com/mss/PD_pushRequest");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Hallgren Networks BB Push Server/1.0");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $appid . ':' . $password);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/related; boundary=mPsbVQo0a68eIL3OAxnm; type=application/xml", "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", "Connection: keep-alive"));

// grab URL and pass it to the browser
$xmldata = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

//Start parsing response into XML data that we can read and output
$p = xml_parser_create();
xml_parse_into_struct($p, $xmldata, $vals);
$errorcode = xml_get_error_code($p);
if ($errorcode > 0) {
    echo xml_error_string($errorcode);
    $err = true;
}
xml_parser_free($p);

echo 'Our PUSH-ID: ' . $messageid . "<br \>\n";
if (!$err && $vals[1]['tag'] == 'PUSH-RESPONSE') {
    echo 'PUSH-ID: ' . $vals[1]['attributes']['PUSH-ID'] . "<br \>\n";
    echo 'REPLY-TIME: ' . $vals[1]['attributes']['REPLY-TIME'] . "<br \>\n";
    echo 'Response CODE: ' . $vals[2]['attributes']['CODE'] . "<br \>\n";
    echo 'Response DESC: ' . $vals[2]['attributes']['DESC'] . "<br \> \n";
} else {
    echo '<p>An error has occured</p>' . "\n";
    echo 'Error CODE: ' . $vals[1]['attributes']['CODE'] . "<br \>\n";
    echo 'Error DESC: ' . $vals[1]['attributes']['DESC'] . "<br \>\n";
}

 

If you find this post useful please give kudo's
Please use plain text.
New Contributor
hp96cc
Posts: 2
Registered: ‎03-28-2010
My Carrier: Vodafone

Re: Push API sample code needed

Thanks. Thats really useful.

 

How are you getting on with C# version, do you have any sample yets?

 

For anyone who is interested, I am building a multi smartphone platform for sending PUSH messages to all popular platforms.

 

I have built the iPhone version, next is Blackberry, then Android and Win Mo (version 7 onwards). If any ones fancies helping and sharing the code, mail me.

 

Cheers,

 

Craig

 

Please use plain text.
Developer
challgren
Posts: 70
Registered: ‎05-04-2009
My Carrier: T-Mobile/Wifi

Re: Push API sample code needed

Sorry but PHP and BB are my languages I code in.

If you find this post useful please give kudo's
Please use plain text.
Developer
challgren
Posts: 70
Registered: ‎05-04-2009
My Carrier: T-Mobile/Wifi

Re: Push API sample code needed

Here's some updated code. Improves error handling and includes HTML form

 

<?php
if ($_POST) {
    // APP ID provided by RIM
    $appid = '';
    // Password provided by RIM
    $password = '';
    
    //Deliver before timestamp
    $deliverbefore = gmdate('Y-m-d\TH:i:s\Z', strtotime('+5 minutes'));
    
    //An array of address must be in PIN format or "push_all"
    $addresstosendto[] = '';
    
    $addresses = '';
    foreach ($addresstosendto as $value) {
        $addresses .= '<address address-value="' . $value . '"/>';
    }
    
    // create a new cURL resource
    $err = false;
    $ch = curl_init();
    $messageid = microtime(true);

    $data = '--mPsbVQo0a68eIL3OAxnm'. "\r\n" .
    'Content-Type: application/xml; charset=UTF-8' . "\r\n\r\n" .
    '<?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="' . $messageid . '" deliver-before-timestamp="' . $deliverbefore . '" source-reference="' . $appid . '">'
    . $addresses .
    '<quality-of-service delivery-method="unconfirmed"/>
    </push-message>
    </pap>' . "\r\n" .
    '--mPsbVQo0a68eIL3OAxnm' . "\r\n" .
    'Content-Type: text/plain' . "\r\n" .
    'Push-Message-ID: ' . $messageid . "\r\n\r\n" .
    stripslashes($_POST['message']) . "\r\n" .
    '--mPsbVQo0a68eIL3OAxnm--' . "\n\r";

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "https://pushapi.eval.blackberry.com/mss/PD_pushRequest");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, "Hallgren Networks BB Push Server/1.0");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, $appid . ':' . $password);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/related; boundary=mPsbVQo0a68eIL3OAxnm; type=application/xml", "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", "Connection: keep-alive"));

    // grab URL and pass it to the browser
    $xmldata = curl_exec($ch);
    
    // close cURL resource, and free up system resources
    curl_close($ch);
    
    //Start parsing response into XML data that we can read and output
    $p = xml_parser_create();
    xml_parse_into_struct($p, $xmldata, $vals);
    $errorcode = xml_get_error_code($p);
    if ($errorcode > 0) {
        $err = true;
    }
    xml_parser_free($p);

    echo 'Our PUSH-ID: ' . $messageid . "<br \>\n";
    if (!$err && $vals[1]['tag'] == 'PUSH-RESPONSE') {
        echo 'PUSH-ID: ' . $vals[1]['attributes']['PUSH-ID'] . "<br \>\n";
        echo 'REPLY-TIME: ' . $vals[1]['attributes']['REPLY-TIME'] . "<br \>\n";
        echo 'Response CODE: ' . $vals[2]['attributes']['CODE'] . "<br \>\n";
        echo 'Response DESC: ' . $vals[2]['attributes']['DESC'] . "<br \> \n";
    } elseif ($err) {
        echo '<p>An XML parser error has occured</p>' . "\n";
        echo '<pre>' . xml_error_string($errorcode) ."</pre>\n";
        echo '<p>Response</p>' . "\n";
        echo '<pre>' . $xmldata . '</pre>' . "\n"
    } else {
        echo '<p>An error has occured</p>' . "\n";
        echo 'Error CODE: ' . $vals[1]['attributes']['CODE'] . "<br \>\n";
        echo 'Error DESC: ' . $vals[1]['attributes']['DESC'] . "<br \>\n";
    }
} else {
    showhtml();
}

function showhtml() {
    ?>
<html>
<head><title>Blackberry PUSH Bishes!!!</title></head>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p>Message to push:</p>
<textarea name="message" rows="30" cols="50"></textarea>
<p></p><input type="submit" value="Push Data"></p>
</form>
</body>
</html>
<?php
}
?>

 

If you find this post useful please give kudo's
Please use plain text.
Developer
idonen
Posts: 43
Registered: ‎10-03-2008

Re: Push API sample code needed

[ Edited ]

Here's some c# code that in theory should be doing the same thing (built from a combination of examples, including the one above), but all I can get is a 401 unauthorized out of it (i've tried using both my appID and portal userID for the userName, no luck), and the request is coming from my authorized IP. Anyone see any other issues?

 

 

HttpWebResponse HttpWRes = null;
HttpWebRequest HttpWReq = null;

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

string userName = "xxxxx";
string userPW = "xxxxx";

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

 

 

Please use plain text.