Пример #1
0
  /** For debug - and possibly show the info, allow device selection. */
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

    GeatteRegisterRequestInfo reqInfo =
        GeatteRegisterRequestInfo.processRequest(req, resp, getServletContext());
    if (reqInfo == null) {
      return;
    }

    resp.setContentType("application/json");
    JSONObject regs = new JSONObject();
    try {
      regs.put("userEmail", reqInfo.getUserEmail());

      JSONArray devices = new JSONArray();
      for (DeviceInfo di : reqInfo.devices) {
        JSONObject dijson = new JSONObject();
        dijson.put("key", di.getKey().toString());
        dijson.put("deviceName", di.getDeviceName());
        dijson.put("type", di.getType());
        dijson.put("registrationId", di.getDeviceRegistrationID());
        dijson.put("timestamp", di.getRegistrationTimestamp());
        devices.put(dijson);
      }
      regs.put("devices", devices);

      PrintWriter out = resp.getWriter();
      regs.write(out);
    } catch (JSONException e) {
      throw new IOException(e);
    }
  }
Пример #2
0
  private boolean doRegisterIOSToUrban(DeviceInfo deviceInfo) {
    try {
      URL url =
          new URL(
              "https://go.urbanairship.com/api/device_tokens/"
                  + deviceInfo.getDeviceRegistrationID());

      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("PUT");
      connection.setDoOutput(true);

      String appKey = Config.URBAN_APP_KEY;
      String appMasterSecret = Config.URBAN_APP_MASTERSECRET;

      String authString = appKey + ":" + appMasterSecret;
      String authStringBase64 = Base64.encode(authString.getBytes());
      authStringBase64 = authStringBase64.trim();

      connection.setRequestProperty("Content-type", "application/json");
      connection.setRequestProperty("Authorization", "Basic " + authStringBase64);

      JSONObject object = new JSONObject();
      if (deviceInfo.getDeviceName() != null && !deviceInfo.getDeviceName().isEmpty()) {
        object.put("alias", deviceInfo.getDeviceName());
      }
      object.put("tz", "America/Los_Angeles");

      //	    String jsonBodyString = "{\"alias\": \""+ deviceInfo.getDeviceName() +
      //	    "\", \"tz\": \"America/Los_Angeles\" }";

      OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
      osw.write(object.toString());
      osw.close();

      int responseCode = connection.getResponseCode();
      log.log(
          Level.INFO,
          "RegisterServlet.doRegisterIOSToUrban() : registering to Urban OK, resp = "
              + responseCode);
      return true;
    } catch (Exception e) {
      log.log(
          Level.WARNING, "RegisterServlet.doRegisterIOSToUrban() : Error registering to Urban.", e);
    }
    return false;
  }