您好我正在尝试使用Java向我的设备发送推送消息。但是我在建立ssl连接时已经遇到了问题。 这是迄今为止的代码:
KeyStore keyStore = KeyStore.getInstance("PKCS12");
InputStream key = getClass().getResourceAsStream("apns-dev-key.p12");
char[] c = key.toString().toCharArray();
keyStore.load(getClass().getResourceAsStream("apns-dev-cert.p12"), c);
KeyManagerFactory keyMgrFactory = KeyManagerFactory.getInstance("SunX509");
keyMgrFactory.init(keyStore, c);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyMgrFactory.getKeyManagers(), null, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port);
String[] cipherSuites = sslSocket.getSupportedCipherSuites();
sslSocket.setEnabledCipherSuites(cipherSuites);
sslSocket.startHandshake();
我得到的错误是:
java.io.IOException: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded
我猜apns-dev-key.p12文件存在一些问题。任何提示?
上述代码取自:http://undermypalapa.wordpress.com/2009/08/23/apple-push-notification-service-java/
答案 0 :(得分:3)
这是我的工作示例:
private String token = "<token>";
private String host = "gateway.sandbox.push.apple.com";
private int port = 2195;
private String payload = "{\"aps\":{\"alert\":\"Message from Java o_O\"}}";
public APNSender() {
try {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(getClass().getResourceAsStream("cert.p12"), "<password>".toCharArray());
KeyManagerFactory keyMgrFactory = KeyManagerFactory.getInstance("SunX509");
keyMgrFactory.init(keyStore, "<password>".toCharArray());
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyMgrFactory.getKeyManagers(), null, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(host, port);
String[] cipherSuites = sslSocket.getSupportedCipherSuites();
sslSocket.setEnabledCipherSuites(cipherSuites);
sslSocket.startHandshake();
char[] t = token.toCharArray();
byte[] b = Hex.decodeHex(t);
OutputStream outputstream = sslSocket.getOutputStream();
outputstream.write(0);
outputstream.write(0);
outputstream.write(32);
outputstream.write(b);
outputstream.write(0);
outputstream.write(payload.length());
outputstream.write(payload.getBytes());
outputstream.flush();
outputstream.close();
} catch (Exception exception) {
exception.printStackTrace();
}
}
我仍然很好奇的是如何接收错误代码。我用
试了一下InputStream in = sslSocket.getInputStream();
[...]
但没有成功。
Apple docs表示没有错误发送时没有回复,但另一方面他们列出了“没遇到错误”的状态代码。
答案 1 :(得分:0)
您可能需要查看http://code.google.com/p/javapns/。如果你真的需要重新发明轮子而不是直接使用JavaPNS而不是为此编写自己的代码,那么至少JavaPNS的文档应该可以帮助你理解你需要知道的关键原则。