DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • JQueue: A Library to Implement the Outbox Pattern
  • Google Cloud Messaging with Android
  • Cordova: Communicating Between JavaScript and Java
  • Kotlin vs. Java: Which Is the Better Android Development Language?

Trending

  • Extracting Maximum Value From Logs
  • DevSecOps: Integrating Security Into Your DevOps Workflow
  • The API-Centric Revolution: Decoding Data Integration in the Age of Microservices and Cloud Computing
  • Distributed Tracing Best Practices
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. iOS and Android Push Notifications from Java

iOS and Android Push Notifications from Java

Łukasz Budnik user avatar by
Łukasz Budnik
·
May. 23, 13 · Interview
Like (2)
Save
Tweet
Share
86.65K Views

Join the DZone community and get the full member experience.

Join For Free

For 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

Android (robot) Java (programming language) push

Opinions expressed by DZone contributors are their own.

Related

  • JQueue: A Library to Implement the Outbox Pattern
  • Google Cloud Messaging with Android
  • Cordova: Communicating Between JavaScript and Java
  • Kotlin vs. Java: Which Is the Better Android Development Language?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: