Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7556689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:55:34+00:00 2026-05-30T11:55:34+00:00

Hello I am trying to send a push message to my device using Java.

  • 0

Hello I am trying to send a push message to my device using Java. But I’am allready getting problems when establishing the ssl connection.
Here is the code so far:

        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();

The error I am getting is:

java.io.IOException: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded

I guess there is some problem with the apns-dev-key.p12 file. Any hints?

The code above is taken from: http://undermypalapa.wordpress.com/2009/08/23/apple-push-notification-service-java/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:55:36+00:00Added an answer on May 30, 2026 at 11:55 am

    Here my working example:

    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();
        }
    }
    

    What I’m still curious about is how to receive error codes. I tried it with

    InputStream in = sslSocket.getInputStream(); 
    [...]   
    

    but no success.

    The Apple docs say that there is no answer send when no errors occured but on the other hand they list a status code for “No errors encountered”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello there im trying to send files using client-server classes in java. For some
I am trying to send Email using Java Sockets but cannot seem to send
I'm trying to send push notifications to my device using javapns library in liferay
Hello everyone I'm trying to improve my Java skills by solving some problems from
For testing purposes, I am trying to send emails to hello@test.com , but qmail
So I'm trying to send an xml-rpc message to moses xml-rpc server in Java,
Although I'm trying to send an email on port 25 without SSL, I'm getting
I'm trying to send message using tibrvsend command. The scenarios is like below ,
Hello I'm trying to send a simple GET request using flex mx:HTTPService: <mx:HTTPService id=service
Hello I am trying to setup a Padrino project using DataMapper and MySQL on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.