You don't have to be able to write J2ME code to create a useful BlackBerry application. The BlackBerry Browser along with MDS allows you to push data to devices. As ubiquitous as Java is now, not every programmer is in love with the language. But since the BlackBerry is a J2ME-based device, if you want to create a corporate based application, you have to use Java, right? Actually, no. RIM provides excellent hooks into the BlackBerry Browser so that when used in conjunction with a BES, you can create much of the same functionality of a full-blown J2ME application in any language that can use HTTP. You can create applications that push web content to certain devices so that it is there without the user having to pull the content by visiting the page in the BlackBerry Browser. The content that is pushed is cached on the device, making it available whether the user is in a wireless coverage area or not. The Mobile Data Service provides flexibility in how you notify the user that new content has been pushed to the device. This hack will show you how to create a simple push application to give you a jumpstart in the world of MDS Push. You can even test this using the BlackBerry Simulator [Hack #93].
8.7.1. Enable the MDS Push FeatureBy default, the Mobile Data Server is not enabled for push applications. You'll need to enable it and restart the MDS service to turn it on. In BlackBerry Manager, right-click on the server that you'd like to enable and select the "Set as Mobile Data Service Push Server" option. Once enabled, a checkmark appears before the option, as shown in Figure 8-8. Figure 8-8. Enabling the push server8.7.2. Design Your Push ApplicationYou'll need a couple pieces of data to send content to a device. Here is a list of the minimum:
Once you enable the MDS Push Server, a version of Apache Tomcat is made available on the BES to accept HTTP requests from your application. To push content to a device, you'll need to use an HTTP POST to a specific URL on your MDS server. You'll specify the device information in the URL parameters. There are several ways to customize the behavior of your push application through the use of custom HTTP headers in the request. Table 8-1 shows a list of the headers that are available and what functionality they provide.
8.7.3. Push TypeThe X-RIM-Push-Type header determines the location and appearance of the application on the BlackBerry. There are three types of pushes you can make to the device:
Figure 8-9. Browser-Channel push app on Home screenFigure 8-10. Browser-Message push app in message list8.7.4. The Codeuse strict; use LWP::UserAgent; use HTTP::Request; my $MDS = "localhost"; my $PORT = "8080"; my $PIN = '2100000A'; my $url = "http://del.icio.us/html/davemabe/?tags=no&count=20"; my %headers_to_send = ( 'Content-Location' => $url, 'Content-Type' => "text/html", 'X-RIM-Push-Title' => "Dave's del.icio.us Bookmarks", 'X-RIM-Push-Type' => "Browser-Channel", 'X-RIM-Push-Channel-ID' => "daves-delicious", ); my $browser = LWP::UserAgent->new; my $get_response = $browser->get($url); my $content = $get_response->content; my $mds_url = "http://$MDS:$PORT/push?DESTINATION=$PIN&PORT=7874&REQUESTURI=/"; my $request = HTTP::Request->new; $request->method('GET'); $request->uri($mds_url); foreach my $header (keys %headers_to_send) { my $value = $headers_to_send{$header}; $request->header($header,$value); } $request->content($content); my $response = $browser->request($request); if ($response->is_success) { print "Push was successful. Sent to $PIN.\n"; } else { print "There was a problem. Code was: ",$response->code,"\n"; } 8.7.5. Run the CodeType the code into your favorite text editor and save the file as mdspush.pl. Bring up a command prompt and type the following in the directory where you saved the file. C:>perl mdspush.pl 8.7.6. OutputYou'll get different output from this command depending on whether your push was successful or not. If the push was successful, you will get the following output: Push was successful. Sent to 2100000A. If there was a problem with the request, you will get an error message along with the HTTP response code that MDS returned: There was a problem. Code was: 500 In the event the code is unsuccessful, there are some common errors that should be checked. In my testing, I created some errors and got these status codes. An HTTP response code of 500 was given when I used an incorrect URL for my MDS. A response code of 403 indicated I had either sent to the wrong PIN, or the URL for the content I was sending to the device was invalid. When your code is successful, you should get an icon on the device's Home screen, as shown in Figure 8-9, and when you click on the icon, you should see a list of links I've recently posted on my del.icio.us page, as shown in Figure 8-11. Figure 8-11. The pushed content on the deviceAs well as placing an icon on the Home screen, a browser channel push will add an additional folder underneath your BlackBerry Browser's bookmarks that contains all channel applications on the device. The user can use the trackwheel to open, view details, reorder, or delete the channels you push. |