JMS AeroGear Example

This example shows how you can send a message to a mobile device by leveraging AeroGears push technology which provides support for different push notification technologies like Google Cloud Messaging, Apple's APNs or Mozilla's SimplePush.

For this example you will need an AeroGear Application running somewhere, a good way to do this is to deploy the Push Application on openshift, you can follow the AeroGear Push 0.X Quickstart.

Once you have created your AeroGear Push Application you can create a mobile application. Simply log into the application on the web and create a new mobile application by clicking the 'create' button. Once created you will see an application id and a master secret, you will need the later to run the example.

lastly you will need to create a variant. For this example we will be using Android so you will need to create a google project, this article explains how to do this. Once created click on your app then click 'add' to add a variant. choose 'google cloud messaging', enter your google API key and the project number from your google project and click create

Now before we run the example we need a mobile application to receive it. Writing a mobile app is beyond the scope of this example but for testing purposes we have supplied an Android app you can use, simply install on your android phone. It can be found here. For a more in depth mobile app example visit the AeroGear site.

Once you have installed the mobile app you will need to configure the following:

AeroGear Unified Push URL : This is the URL where your aerogear server is running, something like http://myapp-mydomain.rhcloud.com AeroGear Variant ID : This is the ID of the variant you created in AeroGear AeroGear Variant Secret : This is the secret for your variant GCM Sender ID : this is the Google project Number you created on Google Variant : you can use this to target messages if needed.

Once you set all these correctly you should get a message saying your mobile app is registered, if you log into your AeroGear app you should see it registered with the variant.

Now to run the example simply run the following command 'mvn -Dendpoint=my aerogear url -Dapplicationid=my application id -Dmastersecret=my master secret -Djsse.enableSNIExtension=false clean verify'. If you arent using java 7 you can omit the 'jsse.enableSNIExtension=false'

You should see something like this in your ActiveMQServer

               
       Dec 04, 2013 3:25:39 PM org.jboss.aerogear.unifiedpush.SenderClient submitPayload
       INFO: HTTP Response code from UnifiedPush Server: 302
       Dec 04, 2013 3:25:39 PM org.jboss.aerogear.unifiedpush.SenderClient submitPayload
       INFO: Performing redirect to 'https://myapp-mydomain.rhcloud.com/rest/sender/'
       Dec 04, 2013 3:25:40 PM org.jboss.aerogear.unifiedpush.SenderClient submitPayload
       INFO: HTTP Response code from UnifiedPush Server: 200
               
            

And on your mobile app you should see a message from ActiveMQ

Now lets look a bit more closely at the configuration in activemq-configuration.xml

               
       <queues>
           <queue name="jms.queue.exampleQueue">
               <address>jms.queue.exampleQueue</address>
           </queue>
       </queues>
    
       <connector-services>
           <connector-service name="aerogear-connector">
               <factory-class>org.apache.activemq.integration.aerogear.AeroGearConnectorServiceFactory</factory-class>
               <param key="endpoint" value="${endpoint}"/>
               <param key="queue" value="jms.queue.exampleQueue"/>
               <param key="application-id" value="${applicationid}"/>
               <param key="master-secret" value="${mastersecret}"/>
           </connector-service>
       </connector-services>
               
            

Firstly you will see that we have to create a core queue so it is available when the connector is started, the following are mandatory parameters:

  1. endpoint - The endpoint or URL of you AeroGear application
  2. queue - The name of the queue to consume from
  3. application-id - The application id of your mobile application in AeroGear
  4. master-secret - the secret of your mobile application in AeroGear

as well as those there are also the following optional parameters

  1. ttl - The time to live for the message once AeroGear receives it
  2. badge - The badge the mobile app should use for the notification
  3. sound - The sound the mobile app should use for the notification
  4. filter - A message filter(selector) to use on the connector
  5. retry-interval - If an error occurs on send, how long before we try again
  6. retry-attempts - How many times we should try to reconnect after an error
  7. variants - A comma separated list of variants that should get the message
  8. aliases - A list of aliases that should get the message
  9. device-types - A list of device types that should get the message

More in depth explanations of these can be found in the AeroGear docs.

Now lets look at a snippet of code we used to send the message for our JMS client

      
  Queue queue = (Queue)initialContext.lookup("queue/exampleQueue");

  // Step 3. Perform a lookup on the Connection Factory
  ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");

  // Step 4.Create a JMS Connection
  connection = cf.createConnection();

  // Step 5. Create a JMS Session
  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

  // Step 6. Create a JMS Message Producer
  MessageProducer producer = session.createProducer(queue);

  // Step 7. Create a Text Message
  Message message = session.createMessage();

  message.setStringProperty("AEROGEAR_ALERT", "Hello this is a notification from ActiveMQ");

  producer.send(message);
      
  

The most important thing here is string propert we have set on the message, i.e. 'AEROGEAR_ALERT'. This is the actual alert that is sent via AeroGear

As well as the alert itself you can override any of the above optional parameters in the same fashionby using the following propert names: AEROGEAR_SOUND,AEROGEAR_BADGE,AEROGEAR_TTL,AEROGEAR_VARIANTS,AEROGEAR_ALIASES and AEROGEAR_DEVICE_TYPES