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

php Browser Push Sample

by BlackBerry Development Advisor ‎12-20-2010 11:04 AM - edited ‎04-18-2011 01:56 PM (8,116 Views)

Summary

 

This article applies to BlackBerry® devices based on Java® technology.

 

 


 

 

Description

 

php can be used to create a simple, transferable web page that can be used to initiate a Browser Push to the BlackBerry® device.  Accessible from any browser, it allows you to send any type of Browser Push using the RIM push format to any user on a BlackBerry® Enterprise Server.  Please see the .zip file at the end of this article to download the sample php application.  This application also uses an html page that contains a form to collect the required push information from the BlackBerry device user.  This is a basic HTML form and is not discussed below.

The first step in the PHP application is to create the URL that the request will be posted to.

 

$path = "/push?DESTINATION=" . $HTTP_POST_VARS['email'] . "&PORT=7874&REQUESTURI=/";

 

 

The destination points to the email address or BlackBerry PIN number of the user to deliver the Browser Push to.  The port number of 7874 is the port the BlackBerry® Browser is listening on for incoming push requests.  Once this URL has been constructed we open a socket connection to the BlackBerry® MDS.

 

 

$besfs = @fsockopen($HTTP_POST_VARS['besHostname'], $HTTP_POST_VARS['besPort'], $errno, $errstr, 30);

 

 

The BlackBerry MDS Server hostname and MDS push port supplied in the html push form are used as parameters to open the socket connection.  If the connection was successfully opened we can start to write out the header information.

 

 

$besBuffer = "POST $path HTTP/1.0\r\n";
$besBuffer .= "Host: " . $_POST['besHostname'] . "\r\n";
$besBuffer .= "Content-Location: " . $HTTP_POST_VARS['pushURL'] . "\r\n";
$besBuffer .= "X-RIM-Push-Title: " . $HTTP_POST_VARS['pushTitle'] . "\r\n";
$besBuffer .= "X-RIM-Push-Type: " . $HTTP_POST_VARS['pushType'] . "\r\n";

 

 

The standard HTTP request headers are sent along with the two required BlackBerry Push headers.  The URL of the page to be pushed, the push title and push type are retrieved from the html form the user submitted.  If the Browser Push is a Browser Channel or Browser Channel delete, “X-RIM-Push-Channel-ID” is required.  If a Browser Channel Push is being sent the URL of the read and unread icons are specified.  Not specifying a URL for the read and unread icons will cause the device to use its default icons.  The payload to be sent to MDS-CS is stored in a buffer called besBuffer.

 

 

if ($_POST['pushType'] == "Browser-Channel" || $_POST['pushType'] == "Browser-Channel-Delete")
{
$besBuffer .= "X-RIM-Push-Channel-ID: " . $HTTP_POST_VARS['pushURL'] . "\r\n";

//Send the push icon URL's if we are using a browser channel push.
if($_POST['pushType'] == "Browser-Channel")
{
$besBuffer .= "X-RIM-Push-UnRead-Icon-URL: " . $HTTP_POST_VARS['unreadIconURL'] . "\r\n";
$besBuffer .= "X-RIM-Push-Read-Icon-URL: " . $HTTP_POST_VARS['readIconURL'] . "\r\n";
}
}

 

The sample then sets any optional headers that have been specified in the main HTML form.  That section of php code will be skipped from this description.  Please refer to the attached sample for the full complete source code.

 

Now that all required header information has been sent to the BlackBerry MDS Server we are ready to send the web page we are going to push.  Sending the entire web page would not be required when sending a Browser Channel Delete.

 

 

if ($HTTP_POST_VARS['pushType'] != "Browser-Channel-Delete")
{

$webURL = parse_url($HTTP_POST_VARS['pushURL']);

//Set the port to 80 if not otherwise specified in the push URL.
if (empty($webURL['port']))
$port = 80;
else
$port = $webURL['port'];

//Open a socket connection to the webserver hosting the page to push.
if ($webfs = @fsockopen($webURL['host'], $port, $errno, $errstr, 30))
{
$webBuffer = "GET " . $webURL['path'] . " HTTP/1.1\r\n";
$webBuffer .= "Host: " . $webURL['host'] . "\r\n";
$webBuffer .= "Connection: Close\r\n\r\n";

//Send the request to the Web Server.
fputs($webfs, $webBuffer);

//Read and discard the first line from the webserver (should be HTTP/1.1 200 OK).
$firstline = fgets($webfs);

//Check for a 200 response from the web server.
//If it is not 200 quite and spit out an error.
if (!strstr($firstline, "200"))
{
//Response code was not 200. Quit and show an error.
echo "Error accessing URL " . $HTTP_POST_VARS['pushURL'] . ".";
echo "<br/><br/>";
echo $firstline;

fclose($webfs);
fclose($besfs);
exit();
}

//Read in the page to push from the webserver and write it out to the MDS server.

$foundBody = false;

while (!feof($webfs))
{
$lineRead = fgets($webfs);

//Most of the headers from the web server must be stripped out.
//Keep the Content-Type header.
//Keep reading until we find the body of the web page.

$pos = strpos(strtolower($lineRead), "content-type");

//Pass the Content-Type header from the web server to MDS-CS.
if (!$foundBody && $pos !== false)
{
$besBuffer .= $lineRead;
}
else
{
if (!$foundBody)
{
$pos = strpos(strtolower($lineRead), "html");
if ($pos !== false)
{
$foundBody = true;
$htmlBuffer = $lineRead;
}
}
else
{
$htmlBuffer .= $lineRead;
}
}

}

//Calculate the content length and add the content to the besBuffer.
$besBuffer .= "Content-length: " . strlen($htmlBuffer) . "\r\n";
$besBuffer .= "Connection: Close\r\n\r\n";
$besBuffer .= $htmlBuffer;
//Close the connection to the webserver.
fclose($webfs);

}
else
{
echo "Failed to connect to " . $HTTP_POST_VARS['pushURL'] . "<br>";
echo "Error: " . $errno . " " .$errstr;

$keepGoing = false;

}
}
else
{
$besBuffer .= "Content-length: 0\r\n";
$besBuffer .= "Connection: Close\r\n\r\n";
}

 

At this point the push is ready to be sent to the BlackBerry MDS Server.  All that is left is to capture the output from the BlackBerry MDS Server and pass it through to the user performing the push.  A response code of 200 from the BlackBerry MDS Server indicates a successful push has taken place.

 

 

//Send besBuffer to MDS-CS.
fputs($besfs, $besBuffer);

$buf = "";

//Capture the MDS server's reply to the push.
while (!feof($besfs))
$buf .= fgets($besfs,128);
fclose($besfs);

//Display the MDS server's reply.
echo "Response from MDS: <br>";
echo $buf;

 

From the sample code above you can see how little effort is involved in creating a Browser Push application for the BlackBerry device.  Doing so will give you a framework to deploy any type of web application to BlackBerry device users.  Or you can simply place the entire file included in the .zip file below onto a PHP enabled web server to begin pushing to users immediately.

 

Note:  To support the recording of push notification information the sample creates and updates a file called notifications.txt, which is located within the same directory the same runs from.  You may need to configure your web server to allow the sample to create a file in this location.  The pushNotifications.php file processes push notifications and writes to notifications.txt.

 

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4 <!--[if gte mso 10]>

 

//Read and discard the first line from the webserver (should be HTTP/1.1 200 OK).

fgets($webfs);

 

//Read in the page to push from the web server and write it out to the MDS server.

while (!feof($webfs))

{

       fputs($besfs, fgets($webfs));

}

//Close the connection to the web server.

fclose($webfs);

Users Online
Currently online: 40 members 2,370 guests
Please welcome our newest community members: