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
Highlighted
Contributor
Posts: 22
Registered: ‎03-27-2013
My Device: 9520
My Carrier: MoviStar

PHP script to send push notifications for BB OS <= 7.x

[ Edited ]

Hello everyone. I am building a BB OS <= 7.x application and implementring push notifications functionality on it. When i use the low-level-sample , provided by RIM , to send a push , everything works great and i get the push on the device.

However i want to use php on my server and not java.


I am trying this php code that i found on another thread here :

 

<?php
if ($_POST) {
    // APP ID provided by RIM

    $appid = '3582-M4687r9k9k836r970cO2395i32i66y10a34';
    // Password provided by RIM
    $password = '7dZCUDpG';

    //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[] = '320EF999';

    $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"));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    // 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
}
?>

 

The response i get back when i run the script with my credentials is :

 

Our PUSH-ID: 1368454337.0085
PUSH-ID: 1368454337.0085
REPLY-TIME: 2013-05-13T14:12:16Z
Response CODE: 1001
Response DESC: The request has been accepted for processing.

 However in the device  i never get the push. I know is not the device problem , cause with the low-level-sample i get the push. I ve tried 3-4 different scripts that i ve found around but all of them give errors. This is the only one that seems to work , but it doesnt really. The only strange thing if it helps , is that the Reply Time is 2 hours behind my local time(i run the php script locally on my pc). I dont know if this is something or not.

 

Any ideas on this one? Or another php script that should work?

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

Re: PHP script to send push notifications for BB OS <= 7.x

Hi,

 

The reply time is in GMT.  That's why it differs from your local time.

 

You're currently using the eval environment for testing, correct?  If you send me a private message with your app ID, device PIN, and push ID then I can look it up and see what its devliery status is.

 

By the way, you should not be using:

https://pushapi.eval.blackberry.com/mss/PD_pushRequest

 

The new URL should have cpxxx (where you fill in your assigned content provider ID) in it.

It should be in your Push Service registration email that you were sent.

 

I would also set a deliver before timestamp of longer than 5 minutes.  Maybe an hour or so.

 

Thanks,

 

Matt