APNS - 无法使用 Java 向 iOS 设备发送通知

APNS - Can't send notification to iOS device using Java

我需要让它工作,但我完全没有选择...任何帮助将不胜感激。

我正在尝试使用 Java 向我的 iOS 设备发送通知,并且遵循了许多教程和论坛帖子,主要是这些:raywenderlich.com, JavaPNS Basic Example 和 java-apns 示例如下所示。

不过,我可以使用 PHP 发送通知。有人能看到我做错了什么吗?我已经为此工作了好几天,但无法弄清楚问题是什么 b/c 我已经复制了其他人声称有效的确切代码。这是我的代码:

public static void main(String[] args) {
    try {
        // Not my real token
        String token = "2919333333333545F19B427AE7E98307B1B2AA9390A38BC0E533E77E11111111";
        ApnsService service = APNS.newService()
                .withCert("C://code//Dev.p12", "password")  // (path to cert, password)
                .withSandboxDestination()
                //.withProductionDestination()
                .build();

        //System.out.println("Testing the connection...");
        //service.testConnection();
        //System.out.println("...Successful!");

        String payload = APNS.newPayload()
                .alertBody("Java test message!")
                .sound("default")
                .build();

        System.out.println("About to send the message...");
        service.push(token, payload);
        System.out.println("...message sent!");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是我得到的异常:

About to send the message...
com.notnoop.exceptions.NetworkIOException: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at com.notnoop.apns.internal.Utilities.wrapAndThrowAsRuntimeException(Utilities.java:284)
at com.notnoop.apns.internal.ApnsConnectionImpl.sendMessage(ApnsConnectionImpl.java:342)
at com.notnoop.apns.internal.ApnsConnectionImpl.sendMessage(ApnsConnectionImpl.java:312)
at com.notnoop.apns.internal.ApnsServiceImpl.push(ApnsServiceImpl.java:46)
at com.notnoop.apns.internal.AbstractApnsService.push(AbstractApnsService.java:56)
at com.notnoop.apns.internal.ApnsServiceImpl.push(ApnsServiceImpl.java:36)
at com.notnoop.apns.internal.AbstractApnsService.push(AbstractApnsService.java:45)
at utilities.APNSTestUtility.main(APNSTestUtility.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:992)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:747)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
at java.io.OutputStream.write(OutputStream.java:75)
at com.notnoop.apns.internal.ApnsConnectionImpl.sendMessage(ApnsConnectionImpl.java:328)
... 11 more
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(InputRecord.java:505)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
... 16 more

当我输入错误的密码时,我会收到错误密码的异常,因此我知道它正在查找 .p12 文件。我可以使用我在网上找到的 PHP 示例发送通知,但是 Java 代码中一定有我遗漏的东西。我已经尝试了 java-apns 库(如上所示)和 JavaPNS 库,我得到了相同的结果。

我们不应该使用私钥 p12 文件,而应该从您的私钥和从 Apple 下载的证书生成一个 p12。 执行以下 OpenSSL 命令以获取正确的 p12 文件:

developer_identity.cer <= download from Apple
mykey.p12 <= Your private key

openssl x509 -in developer_identity.cer -inform DER -out developer_identity.pem -outform PEM
openssl pkcs12 -nocerts -in mykey.p12 -out mykey.pem
openssl pkcs12 -export -inkey mykey.pem -in developer_identity.pem -out iphone_dev.p12