Friday, July 5, 2013

How to Push Notifications to IOS devices from java

For this we can use javapns jar.

First of all we need to have a certificate file type of .p12  . Follow the steps in https://code.google.com/p/javapns/wiki/PreparingCertificates  for creating this .p12 file.

But the sad point , It is a real headache to continue with this .p12 file with JDK 7 . Perhaps it must be a JDK 7 bug. Anyway still we can continue with a .jks file format of the above created certification file with comfortably in JDK7.

Below explains how to convert a .p12 to .jks

Create an empty JKS store
              keytool -genkey -alias alice -keystore alice.jks
Import alice.p12 into alice.jks
             keytool -v -importkeystore -srckeystore alice.p12 -srcstoretype PKCS12 -destkeystore    alice.jks -deststoretype JKS


Then you can use this .jks and send push notifications to ios .

boolean production = true;
  String password = "this is the password you provide when creating certificate file";
  String keyStroke ="alice.jks";
  AppleNotificationServer jksServer = null;
  try {
   jksServer = new AppleNotificationServerBasicImpl(keyStroke,
     password, ConnectionToAppleServer.KEYSTORE_TYPE_JKS,
     production);
  } catch (KeystoreException keystoreException) {
  
  }
  PushNotificationPayload payload = PushNotificationPayload.complex();
  try {
   payload.addAlert(pushMessage);
  } catch (JSONException e2) {
  
  }
 
  PushNotificationManager pushManager = new PushNotificationManager();
  try {
   pushManager.initializeConnection(jksServer);
  } catch (CommunicationException | KeystoreException e) {
  
  }
  try {
   List notifications = pushManager
     .sendNotifications(payload, "device id here to which the notifications will be sent");
  } catch (CommunicationException | KeystoreException e) {
  
  }


No comments:

Post a Comment