@Override
  public void userStartedBroadcasting(User user, Stream stream) {

    List<User> friends = this.friendshipService.findAllFriendsOfUser(user.getId());
    for (User userToNotify : friends) {
      if (userToNotify == null
          || userToNotify.getAccount() == null
          || !StringUtils.hasText(userToNotify.getAccount().getPushNotificationID())) {
        continue;
      }

      if (ObjectUtils.nullSafeEquals(user, userToNotify)) {
        // do not notify current user
        continue;
      }

      final ParseRequest request = new ParseRequest();
      IOSNotification iosNotification = new IOSNotification(NotificationType.BROADCAST_STARTED);
      iosNotification.setUserId(user.getId());
      iosNotification.setUserName(user.getUserName());
      iosNotification.setAlert(stream.getText());
      iosNotification.setBadge("Increment");
      iosNotification.setCategory("pass_watch");

      final ParseQuery query = new ParseQuery(userToNotify.getAccount().getPushNotificationID());
      request.setWhere(query);
      request.setData(iosNotification);

      try {
        HttpEntity<ParseRequest> requestEntity = new HttpEntity<ParseRequest>(request);
        ParseResponse response =
            this.parseRestTemplate.postForObject(
                PARSE_POST_PUSH_URL, requestEntity, ParseResponse.class);
        if (response.getError() != null) {
          System.out.println("PARSE PUSH NOTIFICATIOn ERROR: " + response.getError());
        }
      } catch (Exception e) {
        log.error("Error while sending notification to Parse for iOS clients", e);
      }
    }
  }
Beispiel #2
0
  /**
   * Performs this ParseCommand by issuing a synchronous network request.
   *
   * @return The response received if the request was successful.
   * @throws ParseException if anything goes wrong.
   */
  public ParseResponse perform() throws ParseException {

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Data to be sent: " + data.toString());
    }

    long commandStart = System.currentTimeMillis();
    final ParseResponse response = new ParseResponse();
    final ConnectionRequest request = createConnectionRequest(response);
    setUpRequest(request);

    if (progressCallback != null) {
      NetworkManager.getInstance()
          .addProgressListener(
              new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                  if (evt instanceof NetworkEvent) {
                    final NetworkEvent networkEvent = (NetworkEvent) evt;
                    if (request.equals(networkEvent.getConnectionRequest())) {
                      int progressPercentage = networkEvent.getProgressPercentage();
                      if (progressPercentage >= 0) {
                        progressCallback.done(progressPercentage);
                      }
                    }
                  }
                }
              });
    }

    Iterator keys = headers.keys();
    while (keys.hasNext()) {
      final String key = (String) keys.next();

      try {
        request.addRequestHeader(key, (String) headers.get(key));
      } catch (JSONException ex) {
        Logger.getInstance().error("Error parsing header '" + key + "' + Error: " + ex);
        throw new ParseException(
            ParseException.INVALID_JSON, ParseException.ERR_PREPARING_REQUEST, ex);
      }
    }

    keys = data.keys();
    while (keys.hasNext()) {
      final String key = (String) keys.next();
      if (!REQUEST_BODY_KEY.equals(key)) {
        try {
          request.addArgument(key, data.get(key).toString());
        } catch (JSONException ex) {
          Logger.getInstance()
              .error("Error parsing key '" + key + "' in command data. Error: " + ex);
          throw new ParseException(
              ParseException.INVALID_JSON, ParseException.ERR_PREPARING_REQUEST, ex);
        }
      }
    }
    NetworkManager.getInstance().addToQueueAndWait(request);
    response.extractResponseData(request);
    long commandReceived = System.currentTimeMillis();

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug(
          "Parse "
              + request.getHttpMethod()
              + " Command took "
              + (commandReceived - commandStart)
              + " milliseconds\n");
    }

    return response;
  }