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
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 HornetQServer
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 HornetQ
Now lets look a bit more closely at the configuration in hornetq-configuration.xml
<queues>
<queue name="jms.queue.aerogearQueue">
<address>jms.queue.aerogearQueue</address>
</queue>
</queues>
<connector-services>
<connector-service name="aerogear-connector">
<factory-class>org.hornetq.integration.aerogear.AeroGearConnectorServiceFactory</factory-class>
<param key="endpoint" value="${endpoint}"/>
<param key="queue" value="jms.queue.aerogearQueue"/>
<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:
as well as those there are also the following optional parameters
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/aerogearQueue");
// 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 HornetQ");
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