@Test
  public void push() {
    JPushClient jpushClient = new JPushClient(MASTERT_SECRET, APPKEY, 3);

    // For push, all you need do is to build PushPayload object.
    PushPayload payload = buildPushObject_all_all_alert("Test"); // Android And Ios Push

    try {
      PushResult result = jpushClient.sendPush(payload);
      System.out.println("Got result - " + result);
      //			LOG.info("Got result - " + result);

    } catch (APIConnectionException e) {
      // Connection error, should retry later
      System.out.println("Connection error, should retry later" + e);
      //			LOG.error("Connection error, should retry later", e);

    } catch (APIRequestException e) {
      // Should review the error, and fix the request
      System.out.println("Should review the error, and fix the request" + e);
      System.out.println("HTTP Status: " + e.getStatus());
      System.out.println("Error Code: " + e.getErrorCode());
      System.out.println("Error Message: " + e.getErrorMessage());
      //			LOG.error("Should review the error, and fix the request", e);
      //			LOG.info("HTTP Status: " + e.getStatus());
      //			LOG.info("Error Code: " + e.getErrorCode());
      //			LOG.info("Error Message: " + e.getErrorMessage());
    }
  }
 @Test
 public void authenticationFail() {
   String masterSecret = "2b38ce69b1de2a7fa95706e2";
   JPushClient client = new JPushClient(masterSecret, appKey);
   PushPayload payload = PushPayload.alertAll(ALERT);
   try {
     client.sendPush(payload);
   } catch (APIConnectionException e) {
     e.printStackTrace();
   } catch (APIRequestException e) {
     assertEquals(AUTHENTICATION_FAIL, e.getErrorCode());
   }
 }
  @Test
  public void appKeyNotExist() {
    String appKey = "dd1066407b044738b6479274";
    JPushClient client = new JPushClient(masterSecret, appKey);
    PushPayload payload = PushPayload.alertAll(ALERT);

    try {
      client.sendPush(payload);
    } catch (APIConnectionException e) {
      e.printStackTrace();
    } catch (APIRequestException e) {
      assertEquals(APPKEY_NOT_EXIST, e.getErrorCode());
    }
  }
  @Test
  public void lackOfParams_messageAndNotificaiton() {
    JsonObject payload = new JsonObject();
    payload.add("platform", Platform.all().toJSON());
    payload.add("audience", Audience.all().toJSON());
    System.out.println("json string: " + payload.toString());

    try {
      _client.sendPush(payload.toString());
    } catch (APIConnectionException e) {
      e.printStackTrace();
    } catch (APIRequestException e) {
      assertEquals(LACK_OF_PARAMS, e.getErrorCode());
    }
  }
  @Test
  public void invalidParams_audience() {
    JsonObject payload = new JsonObject();
    payload.add("platform", Platform.all().toJSON());
    payload.add("audience", new JsonPrimitive("all_audience"));
    payload.add("notification", Notification.alert(ALERT).toJSON());
    System.out.println("json string: " + payload.toString());

    try {
      _client.sendPush(payload.toString());
    } catch (APIConnectionException e) {
      e.printStackTrace();
    } catch (APIRequestException e) {
      assertEquals(INVALID_PARAMS, e.getErrorCode());
    }
  }
  @Test
  public void tooBig() {
    String msgContent =
        "深圳制造厂的朋友告诉我,过去的一年,他们服务了几十家小型创业公司,代工智能手表。不过,今年这些创业公司已经找不到了,庆幸的是,代工厂都是先付款再生产,也就没有损失。可穿戴设备、硬件创新,大潮初起,泥沙俱下,浪潮过后,却是遍地狼藉。国内的智能手环、手表们,如土曼、果壳,在 Jawbone、Google Glass 们引领下,纷纷推出“划时代”的产品,一时间,国内宣称要做可穿戴设备的公司,如过江之鲫。2013 年,不说句硬件创新,不戴款智能手环,都不好意思说自己是站在人文与科技的十字路口。2013 年,身边的朋友纷纷佩戴上了 Jawbone,幸运的人也会戴上传说中的智能手表。不过,现在越来越多的朋友开始放弃这些所谓的可穿戴式设备。";
    PushPayload payload = PushPayload.messageAll(msgContent);
    String content = payload.toString();

    byte[] bytes = content.getBytes();
    System.out.println("len: " + bytes.length);
    try {
      _client.sendPush(payload);
    } catch (APIConnectionException e) {
      e.printStackTrace();
    } catch (APIRequestException e) {
      assertEquals(TOO_BIG, e.getErrorCode());
    }
  }
Example #7
0
  public static void push(String alert) {
    JPushClient jpushClient =
        new JPushClient(Constants.JPUSH_MASTER_SECRET, Constants.JPUSH_APP_KEY, 3);
    // For push, all you need do is to build PushPayload object.
    PushPayload payload = buildPushObject_android_tag_alertWithTitle(alert);

    try {
      PushResult result = jpushClient.sendPush(payload);
      LOG.info("Got result - " + result);

    } catch (APIConnectionException e) {
      // Connection error, should retry later
      LOG.error("Connection error, should retry later", e);

    } catch (APIRequestException e) {
      // Should review the error, and fix the request
      LOG.error("Should review the error, and fix the request", e);
      LOG.info("HTTP Status: " + e.getStatus());
      LOG.info("Error Code: " + e.getErrorCode());
      LOG.info("Error Message: " + e.getErrorMessage());
    }
  }
  @Test
  public void invalidParams_notification_winphone_empty() {
    JsonObject payload = new JsonObject();
    payload.add("platform", Platform.all().toJSON());
    payload.add("audience", Audience.all().toJSON());

    JsonObject notification = new JsonObject();
    JsonObject winphone = new JsonObject();

    notification.add("winphone", winphone);
    payload.add("notification", notification);

    System.out.println("json string: " + payload.toString());

    try {
      _client.sendPush(payload.toString());
    } catch (APIConnectionException e) {
      e.printStackTrace();
    } catch (APIRequestException e) {
      assertEquals(INVALID_PARAMS, e.getErrorCode());
    }
  }
  @Test
  public void invalidParams_notification_android_builderidNotNumber() {
    JsonObject payload = new JsonObject();
    payload.add("platform", Platform.all().toJSON());
    payload.add("audience", Audience.all().toJSON());

    JsonObject notification = new JsonObject();
    JsonObject android = new JsonObject();
    android.add("builder_id", new JsonPrimitive("builder_id_string"));

    notification.add("android", android);
    payload.add("notification", notification);

    System.out.println("json string: " + payload.toString());

    try {
      _client.sendPush(payload.toString());
    } catch (APIConnectionException e) {
      e.printStackTrace();
    } catch (APIRequestException e) {
      assertEquals(INVALID_PARAMS, e.getErrorCode());
    }
  }
  public boolean pushAndroid(String msg, String devId, Map<String, String> extra) {
    // HttpProxy proxy = new HttpProxy("localhost", 3128);
    // Can use this https proxy: https://github.com/Exa-Networks/exaproxy
    PushPayload payload = buildAndroidPayload(msg, devId, extra);
    //		PushPayload payload = buildPushObject_all_all_alert(msg);

    try {
      PushResult result = jpushClient.sendPush(payload);
      LOG.info("Got result - " + result);

    } catch (APIConnectionException e) {
      LOG.error("Connection error. Should retry later. ", e);
      return false;

    } catch (APIRequestException e) {
      LOG.error("Error response from JPush server. Should review and fix it. ", e);
      LOG.info("HTTP Status: " + e.getStatus());
      LOG.info("Error Code: " + e.getErrorCode());
      LOG.info("Error Message: " + e.getErrorMessage());
      LOG.info("Msg ID: " + e.getMsgId());
      return false;
    }
    return true;
  }