GCM onMessage从未调用过。是否由于端口堵塞?怎么找?

时间:2014-03-07 10:34:07

标签: android port google-cloud-messaging

我正在使用GCM进行Android推送通知。我可以注册GCM并在android中获得注册ID。这意味着端口5228,5229或5230中的任何一个必须是开放的才能接收注册ID吗?因此,我们可以说阻塞端口不是问题吗?我将注册ID发送到服务器端并收到。邮件将从服务器端app发送。

但Android应用程序没有收到它。永远不会调用GCMIntentService中的onMessage。请告诉我没有收到邮件的可能原因。如何检查GCM服务器是否收到消息?端口阻塞了问题吗?如果是这样如何识别和纠正它?

我尝试在cmd中telnet localhost 5228,因为连接无法在端口5228上打开而响应。连接失败。这是否意味着端口5228被阻止了?

从服务器应用程序发送消息时,我得到的结果是非空消息ID和空错误代码。这意味着邮件已发送,对吧?

服务器端代码

@WebServlet("/GCMNotification")
public class GCMNotification extends HttpServlet {
  private static final long serialVersionUID = 1L;

  // Put your Google API Server Key here
  private static final String GOOGLE_SERVER_KEY = "AIzaSyAJBGj_8aaNykTNaPxdayggDMyxzArE7gM";
  static final String MESSAGE_KEY = "message";  

  public GCMNotification() {
    super();
  }

  protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);

  }

  protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {


    Result result = null;

    String share = request.getParameter("shareRegId");

    // GCM RedgId of Android device to send push notification
    String regId = "";
    if (share != null && !share.isEmpty()) {
      regId = request.getParameter("regId");
      PrintWriter writer = new PrintWriter("GCMRegId.txt");
      writer.println(regId);
      writer.close();
      request.setAttribute("pushStatus", "GCM RegId Received.");
      request.getRequestDispatcher("index.jsp")
          .forward(request, response);
    } else {

      try {
        BufferedReader br = new BufferedReader(new FileReader(
            "GCMRegId.txt"));
        regId = br.readLine();
        br.close();
        String userMessage = request.getParameter("message");
        Sender sender = new Sender(GOOGLE_SERVER_KEY);
        Message message = new Message.Builder().timeToLive(30)
            .delayWhileIdle(false).addData(MESSAGE_KEY, userMessage).build();
        System.out.println("regId: " + regId);
        result = sender.send(message, regId, 1);
        if(result.getMessageId()==null)
        {
            System.out.println("error"+result.getErrorCodeName());
        }
        else
        {
            System.out.println("msg created"+result.getCanonicalRegistrationId());
            if(result.getCanonicalRegistrationId()==null)
            {

            }
            else
            {
                System.out.println("not null");
            }
        }
        System.out.println("pushStatus: "+result.getMessageId()+"\n"+result.getErrorCodeName()+"\n"+result.toString());
        request.setAttribute("pushStatus", result.toString());
      } catch (IOException ioe) {
        ioe.printStackTrace();
        request.setAttribute("pushStatus",
            "RegId required: " + ioe.toString());
      } catch (Exception e) {
        e.printStackTrace();
        request.setAttribute("pushStatus", e.toString());
      }
      request.getRequestDispatcher("index.jsp")
          .forward(request, response);
    }
  }
}

0 个答案:

没有答案