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) {
  
  }


Tuesday, July 2, 2013

How to push notification to Android from java


 

For this we need the below jars basically.


gcm-server jar

json-simple jar


you can get the latest versions of the above.


And  lets get in to the business directly.


Sender sender = new Sender("senderId"); // follow this link to get your id http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
List devicesList = new ArrayList();

// deviceIds are the ids of the devices we send notifications. This is a 64 bit id. Note that this is not the imi nunber. Google search on how to find this.Device to device it varies.I guess.
devicesList.add("deviceId1");
devicesList.add("deviceId2");


com.google.android.gcm.server.Message message = new com.google.android.gcm.server.Message.Builder()
         .collapseKey("1").timeToLive(3).delayWhileIdle(true)
      .addData("message", "sending message")
      .build();


MulticastResult result = sender.send(message, devices, 1);



And that's it .Thank you.