Hack 91. Delete a Push Application

Once a push application is no longer needed, be courteous and clean up after yourself. Don't litter send the unused icon to the bit bucket!

We all would like to create useful, elegant software applications that users love and that last forever. Usually it doesn't turn out so rosy. There are bug fixes, new features, and upgrades that are generally required. There are some applications that simply outlive their usefulness.

Once your application is due for a major upgrade or you need to send your program "out to pasture," it's a good idea to clean up after yourself. Once your users start installing some of the third-party applications in this book, their Home screens (or Applications submenus on 7100 series devices) will quickly become used-car lots of icons. You don't want to add to the junk pile. Use this code to push a command that deletes your push application.

8.8.1. Identify Your Application

When you pushed your application for the first time, in addition to a display name, you included a string that uniquely identifies your application on the user's handheld using the custom X-RIM-Push-Channel-ID HTTP header. This string is not seen by the user, but the device knows to use this string as the program's identity. When new content is pushed to the BlackBerry, the device checks whether it has already received content for the application and created a channel for it. If this channel has already been created, the device updates the channel with your new content. If your application's ID doesn't exist on the device, it sets up a new channel for your program.

This same string is used when you need to delete your application (or channel) from a device.

8.8.2. The Code

The code for deleting your application looks much the same as the code for creating one [Hack #90]. This code deletes the push application that was created in that hack that sent the latest links from my del.icio.us page [Hack #46].

 use strict; use LWP::UserAgent; use HTTP::Request; my $MDS = "localhost"; my $PORT = "8080"; my $PIN = '2100000A'; my %headers_to_send = (         'X-RIM-Push-Type' => "Browser-Channel-Delete", 'X-RIM-Push-Channel-ID'  => "daves-delicious",     ); 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);     } my $content = "this string can be anything";  $request->content($content); my $browser = LWP::UserAgent->new;  $browser->agent("My Test Push Application"); my $response = $browser->request($request); if ($response->is_success) {  print "Delete was successful. Sent to $PIN.\n";  } else {          print "There was a problem. Code was: ",$response->code,"\n";      } 

8.8.3. Running the Code

Type the previous code in your favorite text editor and save the file as pushdelete.pl. Bring up a command prompt and type the following in the directory where you saved the file.

 C:>perl pushdelete.pl  

8.8.4. Output

You'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 

BlackBerry Hacks
Blackberry Hacks: Tips & Tools for Your Mobile Office
ISBN: 0596101155
EAN: 2147483647
Year: 2006
Pages: 164
Authors: Dave Mabe

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net