/** 发送jpush到客户端 */
  @Override
  public boolean sendByRegisitrationID(
      String msg, Map<String, String> extras, Integer imsi, String... regisitrationIDs) {

    String extrasJson = new Gson().toJson(extras);
    // extrasJson = extrasJson.replaceAll("\"", "'");
    PushPayload payload = null;
    if (imsi == IMSI_ANDROID) {
      payload =
          PushPayload.newBuilder()
              .setPlatform(Platform.all())
              .setAudience(Audience.registrationId(regisitrationIDs))
              .setMessage(
                  Message.newBuilder().setMsgContent(msg).addExtra("data", extrasJson).build())
              .build();
    }
    //		else if(imsi==IMSI_IOS){
    //			 payload = PushPayload.newBuilder()
    //					.setPlatform(Platform.all())
    //					.setAudience(Audience.registrationId(regisitrationIDs))
    //					.setNotification(Notification.ios(msg, extras)) //alert(msg)
    //					.build();
    //		}
    else { // 默认按iphone处理
      payload =
          PushPayload.newBuilder()
              .setPlatform(Platform.all())
              .setAudience(Audience.registrationId(regisitrationIDs))
              .setNotification(Notification.ios(msg, extras)) // alert(msg)
              .build();
    }

    //		PushPayload payload = PushPayload
    //				.newBuilder()
    //				.setPlatform(Platform.all())
    //				.setAudience(Audience.registrationId(regisitrationIDs))
    //				.setMessage(
    //						Message.newBuilder().setMsgContent(msg)
    //								.addExtra("data",  extrasJson).build()).build();

    //
    //		PushPayload payload = PushPayload.newBuilder()
    //				.setPlatform(Platform.all())
    //				.setAudience(Audience.registrationId(regisitrationIDs))
    //				.setNotification(Notification.ios(msg, extras)) //alert(msg)
    //				.setMessage(
    //						Message.newBuilder().setMsgContent(msg)
    //								.addExtra("data",  extrasJson).build())
    //				.build();
    try {
      PushResult result = jpushClient.sendPush(payload);
      return result.isResultOK();
    } catch (Exception e1) {
      errorLogger.writeErrorLog(e1.getMessage());
      appLogger.error(e1.getMessage(), e1);
      debugLogger.error(e1.getMessage(), e1);
      return false;
    }
  }
  public PushResult sendPush(PushPayload pushPayload)
      throws APIConnectionException, APIRequestException {
    if (_globalSettingEnabled) {
      pushPayload.resetOptionsTimeToLive(_timeToLive);
      pushPayload.resetOptionsApnsProduction(_apnsProduction);
    }

    ResponseWrapper response = _httpClient.sendPost(_baseUrl, pushPayload.toString(), _authCode);

    return PushResult.fromResponse(response);
  }
Exemple #3
0
 private static PushPayload buildPushObject_android_tag_alertWithTitle(String alert) {
   return PushPayload.newBuilder()
       .setPlatform(Platform.android())
       .setAudience(Audience.tag("profit4all"))
       .setNotification(Notification.android(alert, TITLE, null))
       .build();
 }
 /**
  * 构建推送对象:所有平台,推送目标是别名为 "alias1",通知内容为 ALERT。
  *
  * @return
  */
 private static PushPayload buildPushObject_all_alias_alert(String title, String message) {
   return PushPayload.newBuilder()
       .setPlatform(Platform.all())
       .setAudience(Audience.alias(title))
       .setNotification(Notification.alert(message))
       .build();
 }
 /**
  * 构建推送对象:平台是 Android,目标是 tag 为 "tag1" 的设备,内容是 Android 通知 ALERT,并且标题为 TITLE。
  *
  * @return
  */
 private static PushPayload buildPushObject_android_tag_alertWithTitle(
     String target, String title, String message) {
   return PushPayload.newBuilder()
       .setPlatform(Platform.android())
       .setAudience(Audience.tag(target))
       .setNotification(Notification.android(title, message, null))
       .build();
 }
  @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());
    }
  }
  public PushPayload buildAndroidPayload(String msg, String devId, Map<String, String> extra) {
    Message.Builder msgBuilder = Message.newBuilder().setMsgContent(msg);
    if (extra != null) msgBuilder.addExtras(extra);

    return PushPayload.newBuilder()
        .setPlatform(Platform.android())
        .setAudience(Audience.registrationId(devId))
        // .setNotification(Notification.android(msg, msg, extra))
        .setMessage(msgBuilder.build())
        .build();
  }
 /**
  * 构建推送对象:平台是 Andorid 与 iOS,推送目标是 ("tag1" 与 "tag2" 的并集) 且("alias1" 与 "alias2" 的并集),推送内容是 - 内容为
  * MSG_CONTENT 的消息, 并且附加字段 from = JPush。
  *
  * @return
  */
 private static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
   return PushPayload.newBuilder()
       .setPlatform(Platform.android_ios())
       .setAudience(
           Audience.newBuilder()
               .addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
               .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
               .build())
       .setMessage(
           Message.newBuilder().setMsgContent("MSG_CONTENT").addExtra("from", "JPush").build())
       .setOptions(Options.newBuilder().setApnsProduction(PRODUCT_MODE).build())
       .build();
 }
 @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());
    }
  }
  /** 发送jpush到工作端口 */
  @Override
  public boolean sendByRegisitrationIDToWorker(
      String msg, Map<String, String> extras, String... regisitrationIDs) {

    // PushPayload payload = PushPayload.newBuilder()
    // .setPlatform(Platform.all())
    // .setAudience(Audience.registrationId(regisitrationIDs))
    // .setNotification(Notification.newBuilder()
    // .addPlatformNotification(AndroidNotification.newBuilder()
    // .addExtra("orderID", orderID)
    // .build())
    // .build())
    // .setMessage(Message.content(msg))
    // .build();

    String extrasJson = new Gson().toJson(extras);
    // extrasJson = extrasJson.replaceAll("\"", "'");

    PushPayload payload =
        PushPayload.newBuilder()
            .setPlatform(Platform.all())
            .setAudience(Audience.registrationId(regisitrationIDs))
            // .setNotification(Notification.ios(msg, extras)) //alert(msg)
            .setMessage(
                Message.newBuilder().setMsgContent(msg).addExtra("data", extrasJson).build())
            .build();

    // PushPayload payload = PushPayload.newBuilder()
    // .setPlatform(Platform.all())
    // .setAudience(Audience.registrationId(regisitrationIDs))
    // .setNotification( Notification.android(msg, msg, extras)
    // //.ios(msg, extras)
    // Notification.alert("")
    // ).build();

    try {
      PushResult result = jpushClientForService.sendPush(payload);
      return result.isResultOK();
    } catch (Exception e1) {
      errorLogger.writeErrorLog(e1.getMessage());
      appLogger.error(e1.getMessage(), e1);
      debugLogger.error(e1.getMessage(), e1);
      return false;
    }
  }
 @Override
 public boolean sendAll(String msg) {
   PushPayload payload =
       PushPayload.newBuilder()
           .setPlatform(Platform.all())
           .setAudience(Audience.all())
           .setNotification(Notification.alert(msg))
           .build();
   try {
     PushResult result = jpushClient.sendPush(payload);
     return result.isResultOK();
   } catch (Exception e1) {
     errorLogger.writeErrorLog(e1.getMessage());
     appLogger.error(e1.getMessage(), e1);
     debugLogger.error(e1.getMessage(), e1);
     return false;
   }
 }
 /**
  * 构建推送对象:平台是 iOS,推送目标是 "tag1", "tag_all" 的交集, 推送内容同时包括通知与消息 - 通知信息是 ALERT,角标数字为 5,通知声音为 "happy",
  * 并且附加字段 from = "JPush";消息内容是 MSG_CONTENT。通知是 APNs 推送通道的, 消息是 JPush 应用内消息通道的。APNs
  * 的推送环境是“生产”(如果不显式设置的话,Library 会默认指定为开发)
  *
  * @return
  */
 private static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(
     String target,
     String targetAll,
     String messageTitle,
     String message,
     String otherTitle,
     String otherMessage) {
   return PushPayload.newBuilder()
       .setPlatform(Platform.ios())
       .setAudience(Audience.tag_and(target, targetAll))
       .setNotification(
           Notification.newBuilder()
               .addPlatformNotification(
                   IosNotification.newBuilder()
                       .setAlert(messageTitle)
                       .setBadge(5)
                       .setSound("happy.caf")
                       .addExtra(otherTitle, otherMessage)
                       .build())
               .build())
       .setMessage(Message.content(message))
       .setOptions(Options.newBuilder().setApnsProduction(PRODUCT_MODE).build())
       .build();
 }
 /**
  * 快捷地构建推送对象:所有平台,所有设备,内容为 ALERT 的通知。
  *
  * @return
  */
 private static PushPayload buildPushObject_all_all_alert(String message) {
   return PushPayload.alertAll(message);
 }
 public PushPayload buildPushObject_all_all_alert(String msg) {
   return PushPayload.alertAll(msg);
 }