Posts Tagged ‘notifications’
Apple Push Notification Service & Java
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!