0

我正在尝试使用 Apple 的推送通知。我在这里使用本教程:http: //www.ibm.com/developerworks/java/library/mo-ios-push/#ibm-pcon

我正在尝试创建推送通知。示例代码在这里: http ://code.google.com/p/javapns/wiki/PushNotificationBasic ,看起来像这样:

import javapns.Push;

public class PushTest {

   public static void main(String[] args) {

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


   }
}

除了 keystore.p12 部分,我已经设置好了所有东西。以下是文档中有关密钥库的内容:

对象密钥库:对密钥库文件或实际密钥库内容的引用。有关如何创建密钥库的更多信息,请参阅准备证书。您可以将以下对象传递给此参数:

  • java.io.File:指向您的密钥库文件的直接指针
  • java.lang.String:本地密钥库文件的路径
  • java.io.InputStream:提供来自密钥库的字节的流
  • byte[]:密钥库的实际字节
  • java.security.KeyStore:实际加载的密钥库

我不只是想在我的计算机上输入密钥库的路径(就像他们在这里使用 Java-PNS 向 iPhone 发送推送通知时出错?),因为我觉得那是不安全的。

我应该使用哪些对象?我倾向于使用 java.security.KeyStore。

最后一点,此代码需要托管在 Amazon Web Service 的 Elastic Beanstalk 上(如果重要的话)。

---------编辑 1------------

我试图输入 Richard J. Ross III 的代码。但在可以了解我的 .p12 文件设置是否正确之前,我首先需要解决有关 JavaPNS(以及我相信的文件结构)的问题。运行下面的代码会给我这个错误:HTTP Status 404 - Servlet IosSendGameServlet is not available. 当我注释掉所有 JavaPNS 语句时,我得到这个错误:(HTTP Status 500 - javax.servlet.ServletException: Parameter recievingAppleDeviceID not found.因为我没有输入参数)这让我相信访问 JavaPNS 的方式存在问题。也许它在我的文件结构中的位置错误?它与 servlet-api(在 lib 中)位于同一位置。也许这是我上传到 AWS Elastic Beanstalk 服务器的方式的问题?

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

    }
}
4

1 回答 1

2

您应该使用InputStream, 并在类路径中有 .p12 文件,并像这样使用:

InputStream keyStore = this.getClass().getResourceAsStream("nameOfMyKeystore.p12");
于 2012-08-14T00:57:51.633 回答