diff --git a/example/main/java/cn/jpush/api/examples/ScheduleExample.java b/example/main/java/cn/jpush/api/examples/ScheduleExample.java index 3141bb5f..8f3b6cbe 100644 --- a/example/main/java/cn/jpush/api/examples/ScheduleExample.java +++ b/example/main/java/cn/jpush/api/examples/ScheduleExample.java @@ -1,6 +1,7 @@ package cn.jpush.api.examples; import cn.jpush.api.JPushClient; +import cn.jpush.api.common.Week; import cn.jpush.api.common.resp.APIConnectionException; import cn.jpush.api.common.resp.APIRequestException; import cn.jpush.api.push.model.PushPayload; @@ -43,6 +44,68 @@ public static void testCreateSingleSchedule() { } } + public static void testCreateDailySchedule() { + JPushClient jPushClient = new JPushClient(masterSecret, appKey); + String name = "test_daily_schedule"; + String start = "2015-08-06 12:16:13"; + String end = "2115-08-06 12:16:13"; + String time = "14:00:00"; + PushPayload push = PushPayload.alertAll("test daily example."); + try { + ScheduleResult result = jPushClient.createDailySchedule(name, start, end, time, push); + LOG.info("schedule result is " + result); + } catch (APIConnectionException e) { + LOG.error("Connection error. Should retry later. ", e); + } 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()); + } + } + + public static void testCreateWeeklySchedule() { + JPushClient jPushClient = new JPushClient(masterSecret, appKey); + String name = "test_weekly_schedule"; + String start = "2015-08-06 12:16:13"; + String end = "2115-08-06 12:16:13"; + String time = "14:00:00"; + Week[] days = {Week.MON, Week.FIR}; + PushPayload push = PushPayload.alertAll("test weekly example."); + try { + ScheduleResult result = jPushClient.createWeeklySchedule(name, start, end, time, days, push); + LOG.info("schedule result is " + result); + } catch (APIConnectionException e) { + LOG.error("Connection error. Should retry later. ", e); + } 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()); + } + } + + public static void testCreateMonthlySchedule() { + JPushClient jPushClient = new JPushClient(masterSecret, appKey); + String name = "test_monthly_schedule"; + String start = "2015-08-06 12:16:13"; + String end = "2115-08-06 12:16:13"; + String time = "14:00:00"; + String[] points = {"01", "02"}; + PushPayload push = PushPayload.alertAll("test monthly example."); + try { + ScheduleResult result = jPushClient.createMonthlySchedule(name, start, end, time, points, push); + LOG.info("schedule result is " + result); + } catch (APIConnectionException e) { + LOG.error("Connection error. Should retry later.", e); + } 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()); + } + } + public static void testDeleteSchedule() { String scheduleId = "95bbd066-3a88-11e5-8e62-0021f652c102"; JPushClient jpushClient = new JPushClient(masterSecret, appKey); diff --git a/src/main/java/cn/jpush/api/JPushClient.java b/src/main/java/cn/jpush/api/JPushClient.java index c2a42c1d..9f9f5f41 100644 --- a/src/main/java/cn/jpush/api/JPushClient.java +++ b/src/main/java/cn/jpush/api/JPushClient.java @@ -5,6 +5,7 @@ import cn.jpush.api.common.ClientConfig; import cn.jpush.api.common.TimeUnit; +import cn.jpush.api.common.Week; import cn.jpush.api.common.connection.HttpProxy; import cn.jpush.api.common.resp.APIConnectionException; import cn.jpush.api.common.resp.APIRequestException; @@ -30,6 +31,7 @@ import cn.jpush.api.schedule.ScheduleResult; import cn.jpush.api.schedule.model.SchedulePayload; import cn.jpush.api.schedule.model.TriggerPayload; +import cn.jpush.api.utils.Preconditions; /** * The global entrance of JPush API library. @@ -405,6 +407,17 @@ public DefaultResult deleteAlias(String alias, String platform) return _deviceClient.deleteAlias(alias, platform); } + // ----------------------- Schedule + + /** + * Create a single schedule. + * @param name The schedule name. + * @param time The push time, format is 'yyyy-MM-dd HH:mm:ss' + * @param push The push payload. + * @return The created scheduleResult instance. + * @throws APIConnectionException + * @throws APIRequestException + */ public ScheduleResult createSingleSchedule(String name, String time, PushPayload push) throws APIConnectionException, APIRequestException { TriggerPayload trigger = TriggerPayload.newBuilder() @@ -420,47 +433,198 @@ public ScheduleResult createSingleSchedule(String name, String time, PushPayload return _scheduleClient.createSchedule(payload); } - public ScheduleResult createPeriodicalSchedule(String name, String start, String end, String time, - TriggerPayload.TimeUnit timeUnit, int frequency, String[] point, PushPayload push) + /** + * Create a daily schedule push everyday. + * @param name The schedule name. + * @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'. + * @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'. + * @param time The push time, format 'HH:mm:ss' + * @param push The push payload. + * @return The created scheduleResult instance. + * @throws APIConnectionException + * @throws APIRequestException + */ + public ScheduleResult createDailySchedule(String name, String start, String end, String time, PushPayload push) throws APIConnectionException, APIRequestException { - TriggerPayload trigger = TriggerPayload.newBuilder() - .setPeriodTime(start, end, time) - .setTimeFrequency(timeUnit, frequency, point ) - .buildPeriodical(); - SchedulePayload payload = SchedulePayload.newBuilder() - .setName(name) - .setEnabled(true) - .setTrigger(trigger) - .setPush(push) - .build(); + return createPeriodicalSchedule(name, start, end, time, TimeUnit.DAY, 1, null, push); + } - return _scheduleClient.createSchedule(payload); + /** + * Create a daily schedule push with a custom frequency. + * @param name The schedule name. + * @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'. + * @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'. + * @param time The push time, format 'HH:mm:ss' + * @param frequency The custom frequency. + * @param push The push payload. + * @return The created scheduleResult instance. + * @throws APIConnectionException + * @throws APIRequestException + */ + public ScheduleResult createDailySchedule(String name, String start, String end, String time, int frequency, PushPayload push) + throws APIConnectionException, APIRequestException { + return createPeriodicalSchedule(name, start, end, time, TimeUnit.DAY, frequency, null, push); + } + + /** + * Create a weekly schedule push every week at the appointed days. + * @param name The schedule name. + * @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'. + * @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'. + * @param time The push time, format 'HH:mm:ss' + * @param days The appointed days. + * @param push The push payload. + * @return The created scheduleResult instance. + * @throws APIConnectionException + * @throws APIRequestException + */ + public ScheduleResult createWeeklySchedule(String name, String start, String end, String time, Week[] days, PushPayload push) + throws APIConnectionException, APIRequestException { + Preconditions.checkArgument(null != days && days.length > 0, "The days must not be empty."); + + String[] points = new String[days.length]; + for(int i = 0 ; i < days.length; i++) { + points[i] = days[i].name(); + } + return createPeriodicalSchedule(name, start, end, time, TimeUnit.WEEK, 1, points, push); } + /** + * Create a weekly schedule push with a custom frequency at the appointed days. + * @param name The schedule name. + * @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'. + * @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'. + * @param time The push time, format 'HH:mm:ss'. + * @param frequency The custom frequency. + * @param days The appointed days. + * @param push The push payload. + * @return The created scheduleResult instance. + * @throws APIConnectionException + * @throws APIRequestException + */ + public ScheduleResult createWeeklySchedule(String name, String start, String end, String time, int frequency, Week[] days, PushPayload push) + throws APIConnectionException, APIRequestException { + Preconditions.checkArgument(null != days && days.length > 0, "The days must not be empty."); + + String[] points = new String[days.length]; + for(int i = 0 ; i < days.length; i++) { + points[i] = days[i].name(); + } + return createPeriodicalSchedule(name, start, end, time, TimeUnit.WEEK, frequency, points, push); + } + + /** + * Create a monthly schedule push every month at the appointed days. + * @param name The schedule name. + * @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'. + * @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'. + * @param time The push time, format 'HH:mm:ss'. + * @param points The appointed days. + * @param push The push payload. + * @return The created scheduleResult instance. + * @throws APIConnectionException + * @throws APIRequestException + */ + public ScheduleResult createMonthlySchedule(String name, String start, String end, String time, String[] points, PushPayload push) + throws APIConnectionException, APIRequestException { + Preconditions.checkArgument(null != points && points.length > 0, "The points must not be empty."); + return createPeriodicalSchedule(name, start, end, time, TimeUnit.MONTH, 1, points, push); + } + + /** + * Create a monthly schedule push with a custom frequency at the appointed days. + * @param name The schedule name. + * @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'. + * @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'. + * @param time The push time, format 'HH:mm:ss'. + * @param frequency The custom frequency. + * @param points The appointed days. + * @param push The push payload. + * @return The created scheduleResult instance. + * @throws APIConnectionException + * @throws APIRequestException + */ + public ScheduleResult createMonthlySchedule(String name, String start, String end, String time, int frequency, String[] points, PushPayload push) + throws APIConnectionException, APIRequestException { + Preconditions.checkArgument(null != points && points.length > 0, "The points must not be empty."); + return createPeriodicalSchedule(name, start, end, time, TimeUnit.MONTH, frequency, points, push); + } + + /** + * Get the schedule information by the schedule id. + * @param scheduleId The schedule id. + * @return The schedule information. + * @throws APIConnectionException + * @throws APIRequestException + */ public ScheduleResult getSchedule(String scheduleId) throws APIConnectionException, APIRequestException { return _scheduleClient.getSchedule(scheduleId); } + /** + * Get the schedule list size and the first page. + * @return The schedule list size and the first page. + * @throws APIConnectionException + * @throws APIRequestException + */ public ScheduleListResult getScheduleList() throws APIConnectionException, APIRequestException { return _scheduleClient.getScheduleList(1); } + /** + * Get the schedule list by the page. + * @param page The page to search. + * @return The schedule list of the appointed page. + * @throws APIConnectionException + * @throws APIRequestException + */ public ScheduleListResult getScheduleList(int page) throws APIConnectionException, APIRequestException { return _scheduleClient.getScheduleList(page); } + /** + * Update a schedule by the id. + * @param scheduleId The schedule id to update. + * @param payload The new schedule payload. + * @return The new schedule information. + * @throws APIConnectionException + * @throws APIRequestException + */ public ScheduleResult updateSchedule(String scheduleId, SchedulePayload payload) throws APIConnectionException, APIRequestException { return _scheduleClient.updateSchedule(scheduleId, payload); } + /** + * Delete a schedule by id. + * @param scheduleId The schedule id. + * @throws APIConnectionException + * @throws APIRequestException + */ public void deleteSchedule(String scheduleId) throws APIConnectionException, APIRequestException { _scheduleClient.deleteSchedule(scheduleId); } + private ScheduleResult createPeriodicalSchedule(String name, String start, String end, String time, + TimeUnit timeUnit, int frequency, String[] point, PushPayload push) + throws APIConnectionException, APIRequestException { + TriggerPayload trigger = TriggerPayload.newBuilder() + .setPeriodTime(start, end, time) + .setTimeFrequency(timeUnit, frequency, point ) + .buildPeriodical(); + SchedulePayload payload = SchedulePayload.newBuilder() + .setName(name) + .setEnabled(true) + .setTrigger(trigger) + .setPush(push) + .build(); + + return _scheduleClient.createSchedule(payload); + } + } diff --git a/src/main/java/cn/jpush/api/common/TimeUnit.java b/src/main/java/cn/jpush/api/common/TimeUnit.java index aa9cd8f9..7d6ca3b8 100644 --- a/src/main/java/cn/jpush/api/common/TimeUnit.java +++ b/src/main/java/cn/jpush/api/common/TimeUnit.java @@ -4,7 +4,7 @@ public enum TimeUnit { HOUR, DAY, - MONTH - - + MONTH, + WEEK + } diff --git a/src/main/java/cn/jpush/api/common/Week.java b/src/main/java/cn/jpush/api/common/Week.java new file mode 100644 index 00000000..10e1e11f --- /dev/null +++ b/src/main/java/cn/jpush/api/common/Week.java @@ -0,0 +1,11 @@ +package cn.jpush.api.common; + +public enum Week { + MON, + TUE, + WED, + THU, + FIR, + SAT, + SUN +} diff --git a/src/main/java/cn/jpush/api/schedule/model/TriggerPayload.java b/src/main/java/cn/jpush/api/schedule/model/TriggerPayload.java index 7cb6bdb0..94172a38 100644 --- a/src/main/java/cn/jpush/api/schedule/model/TriggerPayload.java +++ b/src/main/java/cn/jpush/api/schedule/model/TriggerPayload.java @@ -1,10 +1,12 @@ package cn.jpush.api.schedule.model; +import cn.jpush.api.common.TimeUnit; import cn.jpush.api.utils.Preconditions; import cn.jpush.api.utils.StringUtils; import cn.jpush.api.utils.TimeUtils; import com.google.gson.*; + public class TriggerPayload implements IModel { private static Gson gson = new Gson(); @@ -56,9 +58,9 @@ public JsonElement toJSON() { p.addProperty("start", start); p.addProperty("end", end); p.addProperty("time", time); - p.addProperty("time_unit", time_unit.name()); + p.addProperty("time_unit", time_unit.name().toLowerCase()); p.addProperty("frequency", frequency); - if( !TimeUnit.day.equals(time_unit) ) { + if( !TimeUnit.DAY.equals(time_unit) ) { JsonArray array = new JsonArray(); for (String aPoint : point) { array.add(new JsonPrimitive(aPoint)); @@ -77,10 +79,6 @@ public static enum Type { single, periodical } - public static enum TimeUnit { - day, week, month - } - public static class Builder{ private String start; diff --git a/src/test/java/cn/jpush/api/schedule/model/TriggerPayloadTest.java b/src/test/java/cn/jpush/api/schedule/model/TriggerPayloadTest.java index 7ecf7b6e..49a75225 100644 --- a/src/test/java/cn/jpush/api/schedule/model/TriggerPayloadTest.java +++ b/src/test/java/cn/jpush/api/schedule/model/TriggerPayloadTest.java @@ -1,6 +1,7 @@ package cn.jpush.api.schedule.model; import cn.jpush.api.FastTests; +import cn.jpush.api.common.TimeUnit; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; @@ -77,7 +78,7 @@ public void testBuildPeriodical() { TriggerPayload trigger = TriggerPayload.newBuilder() .setPeriodTime(start, end, time) - .setTimeFrequency(TriggerPayload.TimeUnit.week, 2, point) + .setTimeFrequency(TimeUnit.WEEK, 2, point) .buildPeriodical(); JsonObject json = new JsonObject(); @@ -85,7 +86,7 @@ public void testBuildPeriodical() { periodical.addProperty("start", start); periodical.addProperty("end", end); periodical.addProperty("time", time); - periodical.addProperty("time_unit", TriggerPayload.TimeUnit.week.name()); + periodical.addProperty("time_unit", TimeUnit.WEEK.name().toLowerCase()); periodical.addProperty("frequency", 2); JsonArray array = new JsonArray(); array.add(new JsonPrimitive("MON")); @@ -106,7 +107,7 @@ public void test_null_start() { TriggerPayload trigger = TriggerPayload.newBuilder() .setPeriodTime(start, end, time) - .setTimeFrequency(TriggerPayload.TimeUnit.week, 2, point) + .setTimeFrequency(TimeUnit.WEEK, 2, point) .buildPeriodical(); } @@ -119,7 +120,7 @@ public void test_empty_end() { TriggerPayload trigger = TriggerPayload.newBuilder() .setPeriodTime(start, end, time) - .setTimeFrequency(TriggerPayload.TimeUnit.week, 2, point) + .setTimeFrequency(TimeUnit.WEEK, 2, point) .buildPeriodical(); }