Under My Palapa

Jason Young’s geek blog

Apple Push Notification Service & Java

with one comment

If you look at the documentation for APNS, it is relatively straightforward. The main thing that is probably new to many Java developers is setting up the SSL connection.

Assuming you have followed the documentation from Apple, you should have a certificate for your Push App, as well as a private key. Export each of these individually into a ‘p12′ file, then cat them together. Not sure if order matters here, I put the certificate first.

KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(getClass().getResourceAsStream(P12FILE), KEYPASS.toCharArray())
KeyManagerFactory keyMgrFactory = KeyManagerFactory.getInstance("SunX509");
keyMgrFactory.init(keyStore, KEYPASS.toCharArray());

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyMgrFactory.getKeyManagers(), null, null);
sslSocketFactory = sslContext.getSocketFactory();

sslSocket = (SSLSocket)sslSocketFactory.createSocket(HOST, PORT);
def cipherSuites = sslSocket.getSupportedCipherSuites();
sslSocket.setEnabledCipherSuites(cipherSuites);
sslSocket.startHandshake();

After this, you can grab the output stream of the socket and write to it. This is what works for me, of course there’s more to managing the socket, but this is the part that was not entirely obvious. You still have to encode your bytes correctly; the service is not very forgiving if you do it wrong. And without any response, you need to pay close attention to what you’re doing!

Advertisement

Written by jaydfwtx

August 23, 2009 at 11:16 pm

Posted in apple, iphone, java

Tagged with , , ,

One Response

Subscribe to comments with RSS.

  1. Thanks. I just wrote a library for interfacing with Apple APNS service. It abstracts away the creation of the certification socket connection as well as the bit packing: http://github.com/notnoop/java-apns

    notnoop

    November 15, 2009 at 4:24 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.