Ejemplo n.º 1
0
  private static boolean doSendViaC2dm(
      String message, String sender, C2DMessaging push, String collapseKey, DeviceInfo deviceInfo) {
    // Trim message if needed.
    if (message.length() > 1000) {
      message = message.substring(0, 1000) + "[...]";
    }

    return push.sendNoRetry(
        deviceInfo.getDeviceRegistrationID(), collapseKey, "sender", sender, "message", message);
  }
  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");

    // Basic XSRF protection
    if (req.getHeader("X-Same-Domain") == null) {
      // TODO: Enable at consumer launch
      // resp.setStatus(400);
      // resp.getWriter().println(ERROR_STATUS + " (Missing X-Same-Domain header)");
      // return;
    }

    String deviceRegistrationID = req.getParameter("devregid");
    if (deviceRegistrationID == null) {
      resp.setStatus(400);
      resp.getWriter().println(ERROR_STATUS + "(Must specify devregid)");
      return;
    }

    User user = checkUser(req, resp, true);
    if (user != null) {
      Key key = KeyFactory.createKey(DeviceInfo.class.getSimpleName(), user.getEmail());
      DeviceInfo device = new DeviceInfo(key, deviceRegistrationID);
      // Context-shared PMF.
      PersistenceManager pm = C2DMessaging.getPMF(getServletContext()).getPersistenceManager();
      try {
        pm.makePersistent(device);
        resp.getWriter().println(OK_STATUS);
      } catch (Exception e) {
        resp.setStatus(500);
        resp.getWriter().println(ERROR_STATUS + " (Error registering device)");
        log.warning("Error registering device.");
      } finally {
        pm.close();
      }
    }
  }
Ejemplo n.º 3
0
  public static String sendMessage(ServletContext context, String recipient, String message) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
      UserService userService = UserServiceFactory.getUserService();
      User user = userService.getCurrentUser();
      String sender = "nobody";
      if (user != null) {
        sender = user.getEmail();
      }
      log.info("sendMessage: sender = " + sender);
      log.info("sendMessage: recipient = " + recipient);
      log.info("sendMessage: message = " + message);

      // ok = we sent to at least one device.
      boolean ok = false;

      // Send push message to phone
      C2DMessaging push = C2DMessaging.get(context);
      boolean res = false;

      String collapseKey = "" + message.hashCode();

      // delete will fail if the pm is different than the one used to
      // load the object - we must close the object when we're done

      List<DeviceInfo> registrations = null;
      registrations = DeviceInfo.getDeviceInfoForUser(recipient);
      log.info("sendMessage: got " + registrations.size() + " registrations");

      // Deal with upgrades and multi-device:
      // If user has one device with an old version and few new ones -
      // the old registration will be deleted.
      if (registrations.size() > 1) {
        // Make sure there is no 'bare' registration
        // Keys are sorted - check the first
        DeviceInfo first = registrations.get(0);
        Key oldKey = first.getKey();
        if (oldKey.toString().indexOf("#") < 0) {
          // multiple devices, first is old-style.
          registrations.remove(0); // don't send to it
          pm.deletePersistent(first);
        }
      }

      int numSendAttempts = 0;
      for (DeviceInfo deviceInfo : registrations) {
        if (!"ac2dm".equals(deviceInfo.getType())) {
          continue; // user-specified device type
        }

        res = doSendViaC2dm(message, sender, push, collapseKey, deviceInfo);
        numSendAttempts++;

        if (res) {
          ok = true;
        }
      }

      if (ok) {
        return "Success: Message sent";
      } else if (numSendAttempts == 0) {
        return "Failure: User " + recipient + " not registered";
      } else {
        return "Failure: Unable to send message";
      }
    } catch (Exception e) {
      return "Failure: Got exception " + e;
    } finally {
      pm.close();
    }
  }