Install this theme
Sending iOS push notifications using Apache Camel and Boxcar.io

Apache Camel is a rule-based routing and mediation engine providing implementation of Enterprise Integration Patterns. It allows message transfer from different sources to different destinations (see http://camel.apache.org/components.html for further details).

Playing around with Apache Camel i’ve decided to write a simple example sending push notifications to the iOS devices. 

One option is to use original Camel’s Apns Component. This requires however your APNS provider has to be registered by Apple in order to send push notifications using generated certificate.

Another option is to use a free, public available APNS provider. In this example I’ll use Boxcar.IO as an APNS provider.

Boxcar allows you to create your own provider (= boxcar account) and send push notifications through it to the free Boxcar App installed on your iOS device. Alternatively (for testing or development purpose) one of the pre-configured, generic providers can be used (i’ll use generic “Monitoring” provider in this post).

Furthermore Boxcar exposes a very simple REST API containing only 3 RESTful requests: SUBSCRIBE, CREATE and BROADCAST. SUBSCRIBE will add your service for a user. CREATE sends a message to one user, or a subset of your users. BROADCAST sends a single message to all of your users.

Let’s prepare the iOS device for using Boxcar first.

  1. Sign up for an account by Boxcar.IO with your email address
  2. Download and install free Boxcar App: http://itunes.apple.com/app/boxcar/id321493542?ign-mpt=uo%3D6&mt=8
  3. Send a SUBSCRIBE request via Boxcar to your iPhone to activate one of the generic providers.
     
    curl -d "[email protected]" http://boxcar.io/devices/providers/MH0S7xOFSwVLNvNhTpiC/notifications/subscribe
     
    

          

    image
  4. Send a dummy CREATE request to your iPhone to test if everything was properly configured.
     
    curl -d "[email protected]&notification[from_screen_name]=Hello&notification[message]=This+is+an+example" http://boxcar.io/devices/providers/MH0S7xOFSwVLNvNhTpiC/notifications
      

Now we are able to send push notifications to our iOS device through Boxcar.

Next step is to create Apache Camel routes by implementing following scenario:

  1. If somebody drops a new file into the predefined folder location, new iOS push notification should be sent.
  2. If somebody sends a HTTP request to our application with the request parameter “msg”, its value will be taken as a message and sent as an iOS push notification.

We’ll start with the Maven dependencies required to fulfill the task above:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core</artifactId>
    <version>2.10.1</version>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-http</artifactId>
    <version>2.10.1</version>
</dependency> 

Next we have to define our 2 route by subclassing Camel’s RouteBuilder:

public class BoxcarRouteBuilder extends RouteBuilder {

    private static final String BOXCAR_QUERY_PREFIX = 
        "[email protected]&notification[from_screen_name]=Javaforge Blog&notification[message]=";

    @Override
    public void configure() throws Exception {

        from("jetty:http://0.0.0.0:8888/messages")
                .setBody(simple(BOXCAR_QUERY_PREFIX + "${in.headers.msg}"))
                .to("log:net.javaforge.blog.camel.LOG")
                .to("http://boxcar.io/devices/providers/MH0S7xOFSwVLNvNhTpiC/notifications?bridgeEndpoint=true")
                .transform().constant("OK");

        from("file:target/inbox?noop=true")
                .setBody(simple(BOXCAR_QUERY_PREFIX + "File added: ${file:path}"))
                .to("log:net.javaforge.blog.camel.LOG")
                .to("http://boxcar.io/devices/providers/MH0S7xOFSwVLNvNhTpiC/notifications");
    }

} 

Some explanations:

  1. First route definition listens for incoming HTTP requests on port 8888, reads the value of the request parameter “msg”, rewrites the message body to the format required by Boxcar and forwards it to the Boxcar’s generic provider. See Apache Camel’s Jetty component for further details: http://camel.apache.org/jetty.html
  2. Second route definition monitors “target/inbox” folder. Once new file is added to it, new message body with the name of the added file will be created and forwarded to the Boxcar. See Apache Camel’s File component for further details: http://camel.apache.org/file2.html

Last step is to start Apache Camel in standalone mode and to provide BoxcarRouteBuilder to it:

public class BoxcarRunner {

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        // enable hangup support so you can press ctrl + c to terminate the JVM
        main.enableHangupSupport();
        // add routes
        main.addRouteBuilder(new BoxcarRouteBuilder());

        // run until you terminate the JVM
        System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n");
        main.run();
    }
} 

Now we can either drop some files into the “target/inbox” folder or send HTTP requests to the localhost:8888/messages like this:

 
curl -d "msg=Hello Iphone" http://localhost:8888/messages
  

Here are some screenshots taken on iPhone after it was bombed with the push notifications :)

image

image
 
  1. joliet75 reblogged this from javaforge and added:
    tested! Working! Thanks!
  2. javaforge posted this
Blog comments powered by Disqus