Beispiel #1
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();
 }
 /**
  * The shortcut of building a simple alert notification object to all platforms and all audiences
  */
 public static PushPayload alertAll(String alert) {
   return new Builder()
       .setPlatform(Platform.all())
       .setAudience(Audience.all())
       .setNotification(Notification.alert(alert))
       .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();
 }
  /** 发送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;
    }
  }
 /**
  * 构建推送对象:平台是 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 lackOfParams_audience() {
    JsonObject payload = new JsonObject();
    payload.add("platform", Platform.all().toJSON());
    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(LACK_OF_PARAMS, e.getErrorCode());
    }
  }
  @Test
  public void invalidParams_platform() {
    JsonObject payload = new JsonObject();
    payload.add("platform", new JsonPrimitive("all_platform"));
    payload.add("audience", Audience.all().toJSON());
    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());
    }
  }
 @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();
 }
  @Override
  public JsonElement toJSON() {
    JsonObject json = new JsonObject();
    if (null != platform) {
      json.add(PLATFORM, platform.toJSON());
    }
    if (null != audience) {
      json.add(AUDIENCE, audience.toJSON());
    }
    if (null != notification) {
      json.add(NOTIFICATION, notification.toJSON());
    }
    if (null != message) {
      json.add(MESSAGE, message.toJSON());
    }
    if (null != options) {
      json.add(OPTIONS, options.toJSON());
    }
    if (null != sms) {
      json.add(SMS, sms.toJSON());
    }

    return json;
  }