iOS and Android Push Notifications from Java
Join the DZone community and get the full member experience.
Join For FreeFor a couple of past weeks I have been working as a Technical Architect in a mobile application for a large bank (or should I say an insurance department of that bank). The application was written in PhoneGap and Sencha Touch. Two platforms were supported: iOS and Android. I was responsible (apart of many other things) for developing notifications for iOS and Android. The notification simply told the insurance agent how many cases & actions were updated/changed.
Sending notifications to iOS devices
In order to use Apple Push Notification Service (APNS), apart of standard developer's certificate and a valid provisioning profile, you need one more thing. You need the APNS ceritificate which is used for mutual authorisation. You can generate it from Apple's iOS Provisioning Portal. Once you have it, you're ready to send notifcations.
APNS is using a binary protocol. Of course you don't have to implement it from the scratch. You can use a very good and throughtly tested notnoop java-apns library which is available here: https://github.com/notnoop/java-apns.
Using it is extremelly simple, the following code works fine (Notification
is my internal class):
@Service @Qualifier("Apns") public class ApnsPushNotificationServiceImpl implements PushNotificationService { private Resource certResource; private String certPassword; private boolean useProductionServer; private String serverAddress; private static final int DEFAULT_APNS_PORT = 2195; public void setCertResource(Resource certResource) { this.certResource = certResource; } public void setCertPassword(String certPassword) { this.certPassword = certPassword; } public void setUseProductionServer(boolean useProductionServer) { this.useProductionServer = useProductionServer; } public void setServerAddress(String serverAddress) { this.serverAddress = serverAddress; } public void pushNotification(Notification notification) { List<Notification> notList = new ArrayList<Notification>(); notList.add(notification); pushNotifications(notList); } public void pushNotifications(List<Notification> notifications) { ApnsService service = createApnsService(); service.start(); for (Notification notification : notifications) { doSendNotification(notification, service); } service.stop(); } private ApnsService createApnsService() { ApnsService service = null; ApnsServiceBuilder serviceBuilder = APNS.newService() .withCert(certResource.getInputStream(), certPassword); if (null != serverAddress && serverAddress.length() > 0) { serviceBuilder.withGatewayDestination(serverAddress, DEFAULT_APNS_PORT); } else if (useProductionServer) { serviceBuilder.withProductionDestination(); } else { serviceBuilder.withSandboxDestination(); } service = serviceBuilder.build(); return service; } private void doSendNotification(Notification notification, ApnsService service) { PayloadBuilder payloadBuilder = APNS.newPayload(); payloadBuilder = payloadBuilder.badge(notification.getBadge()); payloadBuilder = payloadBuilder.sound(notification.getSound()); if (notification.getBody() != null) { payloadBuilder = payloadBuilder.alertBody(notification.getBody()); } String payload = payloadBuilder.build(); service.push(notification.getDeviceToken(), payload); }
Sending notification to Android devices
In comparison to iOS notifications, implemeting the Android Cloud 2 Device Messaging (AC2DM) was a piece of cake. I didn't need any third part library. Thanks to simple and clear Google's RESTful services, I was able to write a code responsible for pushing notifications to Android devices in less than an hour.
The whole process comprises of 2 steps. The first one is authentication, the second one is the actual pushing of a notification. The source code is as follows:
@Service @Qualifier("Ac2dm") public class Ac2dmPushNotificationServiceImpl implements PushNotificationService { private String sendingRoleAccount; private String sendingRolePassword; public void pushNotification(Notification notification) { List<Notification> notList = new ArrayList<Notification<(); notList.add(notification); pushNotifications(notList); } public void pushNotifications(List<Notification> notifications) { try { HttpClient client = new HttpClient(); PostMethod method = new PostMethod( "https://www.google.com/accounts/ClientLogin"); method.addParameter("Email", sendingRoleAccount); method.addParameter("Passwd", sendingRolePassword); method.addParameter("accountType", "HOSTED_OR_GOOGLE"); method.addParameter("source", "unit-test"); method.addParameter("service", "ac2dm"); client.executeMethod(method); byte[] responseBody = method.getResponseBody(); String response = new String(responseBody); String auth = response.split("\n")[2]; String token = auth.split("=")[1]; for (Notification notification : notifications) { doSendNotification(notification, token, client); } } catch (Throwable t) { throw new RuntimeException(t); } } private void doSendNotification(Notification notification, String token, HttpClient client) throws HttpException, IOException { PostMethod method = new PostMethod("https://android.apis.google.com/c2dm/send"); method.addParameter("registration_id", notification.getDeviceToken()); method.addParameter("collapse_key", "collapse"); method.addParameter("data.payload", notification.getBadge().toString()); Header header = new Header("Authorization", "GoogleLogin auth="+token); method.addRequestHeader(header); client.executeMethod(method); byte[] responseBody = method.getResponseBody(); String response = new String(responseBody); } public void setSendingRoleAccount(String sendingRoleAccount) { this.sendingRoleAccount = sendingRoleAccount; } public void setSendingRolePassword(String sendingRolePassword) { this.sendingRolePassword = sendingRolePassword; }
A Note on WebSphere & IBM's Java 5
When the application was deployed to the UAT server and we tried to run integration test responsible for pushing notification to one of our test iPhones, we saw "java.security.InvalidKeyException: Illegal key size" message in the log. After many hours of investigation it turned out that, IBM's Java 5 supports only 512 key sizes. We had to update IBM's Java 5 security jars in order to make it read the APNS certificate.
-1 for IBM!
Summary
It took me some time, but when you see a notifications coming to your device, then using PhoneGap, you launch a JavaScript application which looks like a native app, it makes you feel good :)
cheers,
Łukasz
Opinions expressed by DZone contributors are their own.
Comments