问题
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();
}
}
}
回答1:
You should use an InputStream
, and have the .p12 file in your classpath, and use like this:
InputStream keyStore = this.getClass().getResourceAsStream("nameOfMyKeystore.p12");
来源:https://stackoverflow.com/questions/11944347/ios-push-notification-javapns-keystore-p12-file-security