Skip to content

Commit cd65468

Browse files
committed
modify schedule apis
1 parent eebc969 commit cd65468

File tree

6 files changed

+263
-26
lines changed

6 files changed

+263
-26
lines changed

example/main/java/cn/jpush/api/examples/ScheduleExample.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.jpush.api.examples;
22

33
import cn.jpush.api.JPushClient;
4+
import cn.jpush.api.common.Week;
45
import cn.jpush.api.common.resp.APIConnectionException;
56
import cn.jpush.api.common.resp.APIRequestException;
67
import cn.jpush.api.push.model.PushPayload;
@@ -43,6 +44,68 @@ public static void testCreateSingleSchedule() {
4344
}
4445
}
4546

47+
public static void testCreateDailySchedule() {
48+
JPushClient jPushClient = new JPushClient(masterSecret, appKey);
49+
String name = "test_daily_schedule";
50+
String start = "2015-08-06 12:16:13";
51+
String end = "2115-08-06 12:16:13";
52+
String time = "14:00:00";
53+
PushPayload push = PushPayload.alertAll("test daily example.");
54+
try {
55+
ScheduleResult result = jPushClient.createDailySchedule(name, start, end, time, push);
56+
LOG.info("schedule result is " + result);
57+
} catch (APIConnectionException e) {
58+
LOG.error("Connection error. Should retry later. ", e);
59+
} catch (APIRequestException e) {
60+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
61+
LOG.info("HTTP Status: " + e.getStatus());
62+
LOG.info("Error Code: " + e.getErrorCode());
63+
LOG.info("Error Message: " + e.getErrorMessage());
64+
}
65+
}
66+
67+
public static void testCreateWeeklySchedule() {
68+
JPushClient jPushClient = new JPushClient(masterSecret, appKey);
69+
String name = "test_weekly_schedule";
70+
String start = "2015-08-06 12:16:13";
71+
String end = "2115-08-06 12:16:13";
72+
String time = "14:00:00";
73+
Week[] days = {Week.MON, Week.FIR};
74+
PushPayload push = PushPayload.alertAll("test weekly example.");
75+
try {
76+
ScheduleResult result = jPushClient.createWeeklySchedule(name, start, end, time, days, push);
77+
LOG.info("schedule result is " + result);
78+
} catch (APIConnectionException e) {
79+
LOG.error("Connection error. Should retry later. ", e);
80+
} catch (APIRequestException e) {
81+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
82+
LOG.info("HTTP Status: " + e.getStatus());
83+
LOG.info("Error Code: " + e.getErrorCode());
84+
LOG.info("Error Message: " + e.getErrorMessage());
85+
}
86+
}
87+
88+
public static void testCreateMonthlySchedule() {
89+
JPushClient jPushClient = new JPushClient(masterSecret, appKey);
90+
String name = "test_weekly_schedule";
91+
String start = "2015-08-06 12:16:13";
92+
String end = "2115-08-06 12:16:13";
93+
String time = "14:00:00";
94+
String[] points = {"01", "02"};
95+
PushPayload push = PushPayload.alertAll("test weekly example.");
96+
try {
97+
ScheduleResult result = jPushClient.createMonthlySchedule(name, start, end, time, points, push);
98+
LOG.info("schedule result is " + result);
99+
} catch (APIConnectionException e) {
100+
LOG.error("Connection error. Should retry later. ", e);
101+
} catch (APIRequestException e) {
102+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
103+
LOG.info("HTTP Status: " + e.getStatus());
104+
LOG.info("Error Code: " + e.getErrorCode());
105+
LOG.info("Error Message: " + e.getErrorMessage());
106+
}
107+
}
108+
46109
public static void testDeleteSchedule() {
47110
String scheduleId = "95bbd066-3a88-11e5-8e62-0021f652c102";
48111
JPushClient jpushClient = new JPushClient(masterSecret, appKey);

src/main/java/cn/jpush/api/JPushClient.java

Lines changed: 177 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import cn.jpush.api.common.ClientConfig;
77
import cn.jpush.api.common.TimeUnit;
8+
import cn.jpush.api.common.Week;
89
import cn.jpush.api.common.connection.HttpProxy;
910
import cn.jpush.api.common.resp.APIConnectionException;
1011
import cn.jpush.api.common.resp.APIRequestException;
@@ -30,6 +31,7 @@
3031
import cn.jpush.api.schedule.ScheduleResult;
3132
import cn.jpush.api.schedule.model.SchedulePayload;
3233
import cn.jpush.api.schedule.model.TriggerPayload;
34+
import cn.jpush.api.utils.Preconditions;
3335

3436
/**
3537
* The global entrance of JPush API library.
@@ -405,6 +407,17 @@ public DefaultResult deleteAlias(String alias, String platform)
405407
return _deviceClient.deleteAlias(alias, platform);
406408
}
407409

410+
// ----------------------- Schedule
411+
412+
/**
413+
* Create a single schedule.
414+
* @param name The schedule name.
415+
* @param time The push time, format is 'yyyy-MM-dd HH:mm:ss'
416+
* @param push The push payload.
417+
* @return The created scheduleResult instance.
418+
* @throws APIConnectionException
419+
* @throws APIRequestException
420+
*/
408421
public ScheduleResult createSingleSchedule(String name, String time, PushPayload push)
409422
throws APIConnectionException, APIRequestException {
410423
TriggerPayload trigger = TriggerPayload.newBuilder()
@@ -420,47 +433,198 @@ public ScheduleResult createSingleSchedule(String name, String time, PushPayload
420433
return _scheduleClient.createSchedule(payload);
421434
}
422435

423-
public ScheduleResult createPeriodicalSchedule(String name, String start, String end, String time,
424-
TriggerPayload.TimeUnit timeUnit, int frequency, String[] point, PushPayload push)
436+
/**
437+
* Create a daily schedule push everyday.
438+
* @param name The schedule name.
439+
* @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'.
440+
* @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'.
441+
* @param time The push time, format 'HH:mm:ss'
442+
* @param push The push payload.
443+
* @return The created scheduleResult instance.
444+
* @throws APIConnectionException
445+
* @throws APIRequestException
446+
*/
447+
public ScheduleResult createDailySchedule(String name, String start, String end, String time, PushPayload push)
425448
throws APIConnectionException, APIRequestException {
426-
TriggerPayload trigger = TriggerPayload.newBuilder()
427-
.setPeriodTime(start, end, time)
428-
.setTimeFrequency(timeUnit, frequency, point )
429-
.buildPeriodical();
430-
SchedulePayload payload = SchedulePayload.newBuilder()
431-
.setName(name)
432-
.setEnabled(true)
433-
.setTrigger(trigger)
434-
.setPush(push)
435-
.build();
449+
return createPeriodicalSchedule(name, start, end, time, TimeUnit.DAY, 1, null, push);
450+
}
436451

437-
return _scheduleClient.createSchedule(payload);
452+
/**
453+
* Create a daily schedule push with a custom frequency.
454+
* @param name The schedule name.
455+
* @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'.
456+
* @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'.
457+
* @param time The push time, format 'HH:mm:ss'
458+
* @param frequency The custom frequency.
459+
* @param push The push payload.
460+
* @return The created scheduleResult instance.
461+
* @throws APIConnectionException
462+
* @throws APIRequestException
463+
*/
464+
public ScheduleResult createDailySchedule(String name, String start, String end, String time, int frequency, PushPayload push)
465+
throws APIConnectionException, APIRequestException {
466+
return createPeriodicalSchedule(name, start, end, time, TimeUnit.DAY, frequency, null, push);
467+
}
468+
469+
/**
470+
* Create a weekly schedule push every week at the appointed days.
471+
* @param name The schedule name.
472+
* @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'.
473+
* @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'.
474+
* @param time The push time, format 'HH:mm:ss'
475+
* @param days The appointed days.
476+
* @param push The push payload.
477+
* @return The created scheduleResult instance.
478+
* @throws APIConnectionException
479+
* @throws APIRequestException
480+
*/
481+
public ScheduleResult createWeeklySchedule(String name, String start, String end, String time, Week[] days, PushPayload push)
482+
throws APIConnectionException, APIRequestException {
483+
Preconditions.checkArgument(null != days && days.length > 0, "The days must not be empty.");
484+
485+
String[] points = new String[days.length];
486+
for(int i = 0 ; i < days.length; i++) {
487+
points[i] = days[i].name();
488+
}
489+
return createPeriodicalSchedule(name, start, end, time, TimeUnit.WEEK, 1, points, push);
438490
}
439491

492+
/**
493+
* Create a weekly schedule push with a custom frequency at the appointed days.
494+
* @param name The schedule name.
495+
* @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'.
496+
* @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'.
497+
* @param time The push time, format 'HH:mm:ss'.
498+
* @param frequency The custom frequency.
499+
* @param days The appointed days.
500+
* @param push The push payload.
501+
* @return The created scheduleResult instance.
502+
* @throws APIConnectionException
503+
* @throws APIRequestException
504+
*/
505+
public ScheduleResult createWeeklySchedule(String name, String start, String end, String time, int frequency, Week[] days, PushPayload push)
506+
throws APIConnectionException, APIRequestException {
507+
Preconditions.checkArgument(null != days && days.length > 0, "The days must not be empty.");
508+
509+
String[] points = new String[days.length];
510+
for(int i = 0 ; i < days.length; i++) {
511+
points[i] = days[i].name();
512+
}
513+
return createPeriodicalSchedule(name, start, end, time, TimeUnit.WEEK, frequency, points, push);
514+
}
515+
516+
/**
517+
* Create a monthly schedule push every month at the appointed days.
518+
* @param name The schedule name.
519+
* @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'.
520+
* @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'.
521+
* @param time The push time, format 'HH:mm:ss'.
522+
* @param points The appointed days.
523+
* @param push The push payload.
524+
* @return The created scheduleResult instance.
525+
* @throws APIConnectionException
526+
* @throws APIRequestException
527+
*/
528+
public ScheduleResult createMonthlySchedule(String name, String start, String end, String time, String[] points, PushPayload push)
529+
throws APIConnectionException, APIRequestException {
530+
Preconditions.checkArgument(null != points && points.length > 0, "The points must not be empty.");
531+
return createPeriodicalSchedule(name, start, end, time, TimeUnit.MONTH, 1, points, push);
532+
}
533+
534+
/**
535+
* Create a monthly schedule push with a custom frequency at the appointed days.
536+
* @param name The schedule name.
537+
* @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'.
538+
* @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'.
539+
* @param time The push time, format 'HH:mm:ss'.
540+
* @param frequency The custom frequency.
541+
* @param points The appointed days.
542+
* @param push The push payload.
543+
* @return The created scheduleResult instance.
544+
* @throws APIConnectionException
545+
* @throws APIRequestException
546+
*/
547+
public ScheduleResult createMonthlySchedule(String name, String start, String end, String time, int frequency, String[] points, PushPayload push)
548+
throws APIConnectionException, APIRequestException {
549+
Preconditions.checkArgument(null != points && points.length > 0, "The points must not be empty.");
550+
return createPeriodicalSchedule(name, start, end, time, TimeUnit.MONTH, frequency, points, push);
551+
}
552+
553+
/**
554+
* Get the schedule information by the schedule id.
555+
* @param scheduleId The schedule id.
556+
* @return The schedule information.
557+
* @throws APIConnectionException
558+
* @throws APIRequestException
559+
*/
440560
public ScheduleResult getSchedule(String scheduleId)
441561
throws APIConnectionException, APIRequestException {
442562
return _scheduleClient.getSchedule(scheduleId);
443563
}
444564

565+
/**
566+
* Get the schedule list size and the first page.
567+
* @return The schedule list size and the first page.
568+
* @throws APIConnectionException
569+
* @throws APIRequestException
570+
*/
445571
public ScheduleListResult getScheduleList()
446572
throws APIConnectionException, APIRequestException {
447573
return _scheduleClient.getScheduleList(1);
448574
}
449575

576+
/**
577+
* Get the schedule list by the page.
578+
* @param page The page to search.
579+
* @return The schedule list of the appointed page.
580+
* @throws APIConnectionException
581+
* @throws APIRequestException
582+
*/
450583
public ScheduleListResult getScheduleList(int page)
451584
throws APIConnectionException, APIRequestException {
452585
return _scheduleClient.getScheduleList(page);
453586
}
454587

588+
/**
589+
* Update a schedule by the id.
590+
* @param scheduleId The schedule id to update.
591+
* @param payload The new schedule payload.
592+
* @return The new schedule information.
593+
* @throws APIConnectionException
594+
* @throws APIRequestException
595+
*/
455596
public ScheduleResult updateSchedule(String scheduleId, SchedulePayload payload)
456597
throws APIConnectionException, APIRequestException {
457598
return _scheduleClient.updateSchedule(scheduleId, payload);
458599
}
459600

601+
/**
602+
* Delete a schedule by id.
603+
* @param scheduleId The schedule id.
604+
* @throws APIConnectionException
605+
* @throws APIRequestException
606+
*/
460607
public void deleteSchedule(String scheduleId)
461608
throws APIConnectionException, APIRequestException {
462609
_scheduleClient.deleteSchedule(scheduleId);
463610
}
464611

612+
private ScheduleResult createPeriodicalSchedule(String name, String start, String end, String time,
613+
TimeUnit timeUnit, int frequency, String[] point, PushPayload push)
614+
throws APIConnectionException, APIRequestException {
615+
TriggerPayload trigger = TriggerPayload.newBuilder()
616+
.setPeriodTime(start, end, time)
617+
.setTimeFrequency(timeUnit, frequency, point )
618+
.buildPeriodical();
619+
SchedulePayload payload = SchedulePayload.newBuilder()
620+
.setName(name)
621+
.setEnabled(true)
622+
.setTrigger(trigger)
623+
.setPush(push)
624+
.build();
625+
626+
return _scheduleClient.createSchedule(payload);
627+
}
628+
465629
}
466630

src/main/java/cn/jpush/api/common/TimeUnit.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public enum TimeUnit {
44

55
HOUR,
66
DAY,
7-
MONTH
8-
9-
7+
MONTH,
8+
WEEK
9+
1010
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cn.jpush.api.common;
2+
3+
public enum Week {
4+
MON,
5+
TUE,
6+
WED,
7+
THU,
8+
FIR,
9+
SAT,
10+
SUN
11+
}

src/main/java/cn/jpush/api/schedule/model/TriggerPayload.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package cn.jpush.api.schedule.model;
22

3+
import cn.jpush.api.common.TimeUnit;
34
import cn.jpush.api.utils.Preconditions;
45
import cn.jpush.api.utils.StringUtils;
56
import cn.jpush.api.utils.TimeUtils;
67
import com.google.gson.*;
78

9+
810
public class TriggerPayload implements IModel {
911

1012
private static Gson gson = new Gson();
@@ -56,9 +58,9 @@ public JsonElement toJSON() {
5658
p.addProperty("start", start);
5759
p.addProperty("end", end);
5860
p.addProperty("time", time);
59-
p.addProperty("time_unit", time_unit.name());
61+
p.addProperty("time_unit", time_unit.name().toLowerCase());
6062
p.addProperty("frequency", frequency);
61-
if( !TimeUnit.day.equals(time_unit) ) {
63+
if( !TimeUnit.DAY.equals(time_unit) ) {
6264
JsonArray array = new JsonArray();
6365
for (String aPoint : point) {
6466
array.add(new JsonPrimitive(aPoint));
@@ -77,10 +79,6 @@ public static enum Type {
7779
single, periodical
7880
}
7981

80-
public static enum TimeUnit {
81-
day, week, month
82-
}
83-
8482
public static class Builder{
8583

8684
private String start;

0 commit comments

Comments
 (0)