The UnifiedPush Java Sender Forge Addon

The purpose of this post is to show how easy it is to integrate the UnifiedPush Java Sender into an existing JEE application. As a reminder, the UnifiedPush Java Sender is a library to send unified push messages to an UnifiedPush Server instance that will then submit ¬†this notification to the different vendors networks (GCM, WNS, APNs etc …). The Java Sender will typically be used in a backend system that needs to interact with a mobile application.

JBoss Tools and Forge

We will be using JBDS (JBoss Development studio) that also ships Forge, a powerful RAD tool. Forge can be extended with “Addons” and since the Unified Java Sender 1.1 there is a dedicated addon for it !

Install the UnifiedPush Java Sender Addon

If you are using the Forge console :

 addon-install-from-git --url https://github.com/sebastienblanc/unifiedpush-addon

If you are using Forge tools inside JBDS :

installaddon

Set up the UnifiedPush Addon

Let’s imagine we want to add a Push feature to an existing JEE Web Application, the first thing to do is to setup the addon :

- If you are using the forge console :

unifiedpush-setup

You will have to enter 3 fields :

Server URL:  http://mypushserver.com
PushApplication id:  123456789
Master Secret:

You can also pass directly the options with the command like :

unifiedpush-setup --serverURL http://myserver.com --pushApplicationId 123456789 --masterSecret mypassword

- If you are using Forge Tools :

upssetup

The Java Sender is now ready to be used ! The dependency has been pulled in and a pushConfiguration.json file has been generated and placed in your resources folder (“/META-INF/pushConfiguration.json”).

Now in your project you could do :

PushSender defaultPushSender = DefaultPushSender.withConfig("META-INF/pushConfig.json").build();

And then to send a message :

UnifiedMessage message = UnifiedMessage.withMessage().alert("Hello 1").build();
defaultPushSender.send(message);

Generate the Sender Service

Instead of using directly the Java Sender, you can also let the addon generate a service that will wrap the Java Sender.

- With the console, just run  :

unifiedpush-generate-service

- With Forge Tools just search for the “unifiedpush: Generate Service” command

You will notice that an interface SenderService.java and its implementation, SenderServiceimpl.java has been generated.

You can now inject this service anywhere you want, for instance :

@Inject
private SenderService senderService;

@POST
@Consumes("application/json")
public Response create(Message entity) {
  em.persist(entity);
  UnifiedMessage message = UnifiedMessage.withMessage().alert(entity.getContent()).build();
  senderService.send(message);
  return Response.Ok().build();
}

Conclusion

Let’s conclude this by a short screencast describing all the steps mentioned before :