0

I am trying to use Apple's Push Notifications. I am using this tutorial here: http://www.ibm.com/developerworks/java/library/mo-ios-push/#ibm-pcon

I am on the part where I am trying to create the Push Notification. The sample code is here: http://code.google.com/p/javapns/wiki/PushNotificationBasic and looks like this:

import javapns.Push;

public class PushTest {

   public static void main(String[] args) {

            Push.alert("Hello World!", "keystore.p12", "keystore_password", false, "Your token");


   }
}

I have everything set up except for the keystore.p12 part. Here is what the documentation says about keystores:

Object keystore: a reference to a keystore file, or the actual keystore content. See Preparing certificates for more information about how to create a keystore. You can pass the following objects to this parameter:

  • java.io.File: a direct pointer to your keystore file
  • java.lang.String: a path to your local keystore file
  • java.io.InputStream: a stream providing the bytes from a keystore
  • byte[]: the actual bytes of a keystore
  • java.security.KeyStore: an actual loaded keystore

I do not simply want to type in the path of the keystore on my computer (as they do here Getting error while sending Push Notification to iPhone using Java-PNS?) because I feel like that is unsafe.

Which of the objects should I use? My inclination says to use a java.security.KeyStore.

As a final note, this code needs to be hosted on Amazon Web Service's Elastic Beanstalk (if that matters).

---------Edit 1------------

I have tried to put in Richard J. Ross III's code. But before it is possible to learn if my .p12 file setup is correct, I first need to get around an issue concerning JavaPNS (and file structure I believe). Running the code below gives me this error: HTTP Status 404 - Servlet IosSendGameServlet is not available. When I comment out all of the JavaPNS statements, I get this error: HTTP Status 500 - javax.servlet.ServletException: Parameter recievingAppleDeviceID not found. (because I don't put in the parameters) This leads me to believe that there is a problem with the way that JavaPNS is being accessed. Maybe it is in the wrong place in my file structure? It is in the same place as servlet-api (in lib). Perhaps this is a problem with the way I upload to the AWS Elastic Beanstalk server?

package com.google.android.gcm.demo.server;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javapns.Push; //<--gets commented out to receive 500 error
import javapns.communication.exceptions.CommunicationException;//<--gets commented out to receive 500 error
import javapns.communication.exceptions.KeystoreException;//<--gets commented out to receive 500 error

public class IosSendGameServlet extends BaseServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
            super.init(config);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException,
                    ServletException {
            String recievingAppleDeviceId = getParameter(req,
                            "recievingAppleDeviceID");
            String sendingUser = getParameter(req, "sendingUser");
            InputStream keyStore = this.getClass().getResourceAsStream("/sasSandbox.p12");

            //everything below here gets commented out to receive 500 error
            try {
                    Push.alert("Game with " + sendingUser + ": It's your turn!", keyStore, "mypassword", false,
                                    recievingAppleDeviceId);
            } catch (CommunicationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            } catch (KeystoreException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }

    }
}
Community
  • 1
  • 1
rizzes
  • 1,532
  • 20
  • 33
  • This isn't an error with JavaPNS, it's an error with your servlet code. Add the parameter to your POST (even if it's an invalid value) and try again. – Richard J. Ross III Aug 14 '12 at 20:09
  • Sorry, I must have not been clear enough. The 500 error came when I commented out the JavaPNS package (imports and Push.alert). Like you state above, you can make this error go away by simply inputting the parameters (I was just being lazy and not doing so). But when you uncomment the JavaPNS code, I get the 404 error (HTTP Status 404 - Servlet IosSendGameServlet is not available). I get this error whether or not I add the parameters. The bottom line: the servlet gives a 404 error when JavaPNS is enabled and has no errors when JavaPNS is commented out. – rizzes Aug 15 '12 at 17:22
  • One last note: I would be happy to create a servlet without JavaPNS if that can be done... – rizzes Aug 15 '12 at 17:22
  • Any thoughts Richard? If not, that's fine, I just want to be sure before I move on. – rizzes Aug 21 '12 at 17:33

1 Answers1

2

You should use an InputStream, and have the .p12 file in your classpath, and use like this:

InputStream keyStore = this.getClass().getResourceAsStream("nameOfMyKeystore.p12");
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • Thanks for the initial help. I am still having some issues that you may be able to help me with (see above). Thanks! – rizzes Aug 14 '12 at 17:20