Package org.json.simple

Examples of org.json.simple.JSONObject


  private Object createAlertObject() {
    if (this.hasAlertContent()) {
      if (this.shouldRepresentAlertAsString()) {
        return this.alertBody;
      } else {
        final JSONObject alert = new JSONObject();

        if (this.alertBody != null) {
          alert.put(ALERT_BODY_KEY, this.alertBody);
        }

        if (this.showActionButton) {
          if (this.localizedActionButtonKey != null) {
            alert.put(ACTION_LOC_KEY, this.localizedActionButtonKey);
          }
        } else {
          // To hide the action button, the key needs to be present, but the value needs to be null
          alert.put(ACTION_LOC_KEY, null);
        }

        if (this.localizedAlertKey != null) {
          alert.put(ALERT_LOC_KEY, this.localizedAlertKey);

          if (this.localizedAlertArguments != null) {
            final JSONArray alertArgs = new JSONArray();

            for (final String arg : this.localizedAlertArguments) {
              alertArgs.add(arg);
            }

            alert.put(ALERT_ARGS_KEY, alertArgs);
          }
        }

        if (this.launchImageFileName != null) {
          alert.put(LAUNCH_IMAGE_KEY, this.launchImageFileName);
        }

        return alert;
      }
    } else {
View Full Code Here


    final String alertBody = "This is a test alert message.";

    this.builder.setAlertBody(alertBody);

    {
      final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());

      // The alert property should be a string if all we're specifying is a literal alert message
      assertEquals(alertBody, aps.get("alert"));
    }

    this.builder.setShowActionButton(false);

    {
      final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
      final JSONObject alert = (JSONObject) aps.get("alert");

      assertEquals(alertBody, alert.get("body"));
    }
  }
View Full Code Here

  public void testSetLocalizedAlertMessage() throws ParseException {
    final String alertKey = "test.alert";
    this.builder.setLocalizedAlertMessage(alertKey, null);

    {
      final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
      final JSONObject alert = (JSONObject) aps.get("alert");

      assertEquals(alertKey, alert.get("loc-key"));
      assertNull(alert.get("loc-args"));
    }

    final String[] alertArgs = new String[] { "Moose", "helicopter" };
    this.builder.setLocalizedAlertMessage(alertKey, alertArgs);

    {
      final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
      final JSONObject alert = (JSONObject) aps.get("alert");

      assertEquals(alertKey, alert.get("loc-key"));

      final JSONArray argsArray = (JSONArray) alert.get("loc-args");
      assertEquals(alertArgs.length, argsArray.size());
      assertTrue(argsArray.containsAll(java.util.Arrays.asList(alertArgs)));
    }
  }
View Full Code Here

  @Test
  public void testSetLaunchImage() throws ParseException {
    final String launchImageFilename = "launch.png";
    this.builder.setLaunchImageFileName(launchImageFilename);

    final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
    final JSONObject alert = (JSONObject) aps.get("alert");

    assertEquals(launchImageFilename, alert.get("launch-image"));
  }
View Full Code Here

  @Test
  public void testSetShowActionButton() throws ParseException {
    this.builder.setShowActionButton(true);

    {
      final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());

      assertNull(aps.get("alert"));
    }

    this.builder.setShowActionButton(false);

    {
      final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
      final JSONObject alert = (JSONObject) aps.get("alert");

      assertTrue(alert.keySet().contains("action-loc-key"));
      assertNull(alert.get("action-loc-key"));
    }

    final String actionButtonKey = "action.key";
    this.builder.setLocalizedActionButtonKey(actionButtonKey);
    this.builder.setShowActionButton(true);

    {
      final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
      final JSONObject alert = (JSONObject) aps.get("alert");

      assertEquals(actionButtonKey, alert.get("action-loc-key"));
    }

    this.builder.setShowActionButton(false);

    {
      final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
      final JSONObject alert = (JSONObject) aps.get("alert");

      assertTrue(alert.keySet().contains("action-loc-key"));
      assertNull(alert.get("action-loc-key"));
    }
  }
View Full Code Here

  @Test
  public void testSetLocalizedActionButtonKey() throws ParseException {
    final String actionButtonKey = "action.key";
    this.builder.setLocalizedActionButtonKey(actionButtonKey);

    final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
    final JSONObject alert = (JSONObject) aps.get("alert");

    assertEquals(actionButtonKey, alert.get("action-loc-key"));
  }
View Full Code Here

  @Test
  public void testSetBadgeNumber() throws ParseException {
    final int badgeNumber = 4;
    this.builder.setBadgeNumber(badgeNumber);

    final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());

    assertEquals(badgeNumber, ((Number) aps.get("badge")).intValue());
  }
View Full Code Here

  @Test
  public void testSetSoundFileName() throws ParseException {
    final String soundFileName = "dying-giraffe.aiff";
    this.builder.setSoundFileName(soundFileName);

    final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());

    assertEquals(soundFileName, aps.get("sound"));
  }
View Full Code Here

  @Test
  public void testSetCategoryName() throws ParseException {
    final String categoryName = "INVITE";
    this.builder.setCategoryName(categoryName);

    final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());

    assertEquals(categoryName, aps.get("category"));
  }
View Full Code Here

  }

  @Test
  public void testSetContentAvailable() throws ParseException {
    {
      final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
      assertNull(aps.get("content-available"));
    }

    {
      this.builder.setContentAvailable(true);

      final JSONObject aps = this.extractApsObjectFromPayloadString(this.builder.buildWithDefaultMaximumLength());
      assertEquals(1L, aps.get("content-available"));
    }
  }
View Full Code Here

TOP

Related Classes of org.json.simple.JSONObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.