Search code examples
opensslapple-push-notificationsplayframework-2.2javaapns

Play 2.2 java-apns SSLHandshakeException


My environment is ubuntu 12.04 + Play 2.2.2 + java 7 + java-apns 0.2.3

After I downloaded aps_development from apple and installed to my keyChain, then exported certificate and private key to Dev_Certificates.p12. I put it under conf/certs/Dev_Certificates.p12. This is the error I got:

pool-7-thread-1, WRITE: TLSv1 Handshake, length = 48
pool-7-thread-1, READ: TLSv1 Alert, length = 2
pool-7-thread-1, RECV TLSv1 ALERT:  fatal, handshake_failure
%% Invalidated:  [Session-3, TLS_RSA_WITH_AES_128_CBC_SHA]
pool-7-thread-1, called closeSocket()
pool-7-thread-1, handling exception: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
pool-7-thread-1, called close()
pool-7-thread-1, called closeInternal(true)
Thread-13, handling exception: java.net.SocketException: Socket is closed
[error] application - ConnectionClosed MessageID: -1 Error: UNKNOWN
[error] c.n.a.i.ApnsConnectionImpl - Couldn't send message after 3 retries.Message(Id=1; Token=62303732366363653231353337356337313133303637373437643839333738323631626662356139663037633234386366613034346635643737353031376635; Payload={"aps":{"alert":{"loc-args":["angelokh"],"action-loc-key":"REPLY","loc-key":"NEW_MESSAGE_PUSH_FORMAT"},"badge":0},"extra":{"traversableAgain":true,"empty":false}})
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) ~[na:1.7.0_55]
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:154) ~[na:1.7.0_55]
    at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1959) ~[na:1.7.0_55]
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1077) ~[na:1.7.0_55]
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312) ~[na:1.7.0_55]
    at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:702) ~[na:1.7.0_55]
[error] application - MessageSendFailed Msg: Message(Id=1; Token=62303732366363653231353337356337313133303637373437643839333738323631626662356139663037633234386366613034346635643737353031376635; Payload={"aps":{"alert":{"loc-args":["angelokh"],"action-loc-key":"REPLY","loc-key":"NEW_MESSAGE_PUSH_FORMAT"},"badge":0},"extra":{"traversableAgain":true,"empty":false}}) Exception: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

EDIT1:

The p12 file is placed under conf/certs/. The service is created as def function and called by an akka actor.

def apnsService = { 
    val certPath = "certs/DEV-APNS-CERT.p12"
    val baseBuilder =
          APNS.newService()
            .asBatched()
            .withCert(getClass.getResourceAsStream(certPath), "password")
     baseBuilder.withSandboxDestination()
}.build

Solution

  • The problem is I'm using Akka with Scala. I fixed it by changing to getClass.getClassLoader.

    APNS.newService()
            .asBatched()
            .withCert(getClass.getClassLoader.getResourceAsStream(certPath), "password")