Skip to content

Commit bcb31bf

Browse files
committed
Merge pull request #21 from Liuchy1/master
add schedule update apis
2 parents d99e5da + a1742dd commit bcb31bf

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package cn.jpush.api.examples;
22

33
import cn.jpush.api.JPushClient;
4+
import cn.jpush.api.common.TimeUnit;
45
import cn.jpush.api.common.Week;
56
import cn.jpush.api.common.resp.APIConnectionException;
67
import cn.jpush.api.common.resp.APIRequestException;
78
import cn.jpush.api.push.model.PushPayload;
89
import cn.jpush.api.schedule.ScheduleListResult;
910
import cn.jpush.api.schedule.ScheduleResult;
1011
import cn.jpush.api.schedule.model.SchedulePayload;
12+
import cn.jpush.api.schedule.model.TriggerPayload;
1113
import org.slf4j.Logger;
1214
import org.slf4j.LoggerFactory;
1315

@@ -70,7 +72,7 @@ public static void testCreateWeeklySchedule() {
7072
String start = "2015-08-06 12:16:13";
7173
String end = "2115-08-06 12:16:13";
7274
String time = "14:00:00";
73-
Week[] days = {Week.MON, Week.FIR};
75+
Week[] days = {Week.MON, Week.FRI};
7476
PushPayload push = PushPayload.alertAll("test weekly example.");
7577
try {
7678
ScheduleResult result = jPushClient.createWeeklySchedule(name, start, end, time, days, push);
@@ -145,8 +147,15 @@ public static void testGetScheduleList() {
145147
public static void testUpdateSchedule() {
146148
String scheduleId = "95bbd066-3a88-11e5-8e62-0021f652c102";
147149
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
150+
String[] points = {Week.MON.name(), Week.FRI.name()};
151+
TriggerPayload trigger = TriggerPayload.newBuilder()
152+
.setPeriodTime("2015-08-01 12:10:00", "2015-08-30 12:12:12", "15:00:00")
153+
.setTimeFrequency(TimeUnit.WEEK, 2, points)
154+
.buildPeriodical();
148155
SchedulePayload payload = SchedulePayload.newBuilder()
156+
.setName("test_update_schedule")
149157
.setEnabled(false)
158+
.setTrigger(trigger)
150159
.build();
151160
try {
152161
jpushClient.updateSchedule(scheduleId, payload);

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,88 @@ public ScheduleListResult getScheduleList(int page)
585585
return _scheduleClient.getScheduleList(page);
586586
}
587587

588+
/**
589+
* Update the schedule name
590+
* @param scheduleId The schedule id.
591+
* @param name The new name.
592+
* @return The schedule information after updated.
593+
* @throws APIConnectionException
594+
* @throws APIRequestException
595+
*/
596+
public ScheduleResult updateScheduleName(String scheduleId, String name)
597+
throws APIConnectionException, APIRequestException {
598+
SchedulePayload payload = SchedulePayload.newBuilder()
599+
.setName(name)
600+
.build();
601+
602+
return updateSchedule(scheduleId, payload);
603+
}
604+
605+
/**
606+
* Enable the schedule.
607+
* @param scheduleId The schedule id.
608+
* @return The schedule information after updated.
609+
* @throws APIConnectionException
610+
* @throws APIRequestException
611+
*/
612+
public ScheduleResult enableSchedule(String scheduleId)
613+
throws APIConnectionException, APIRequestException {
614+
SchedulePayload payload = SchedulePayload.newBuilder()
615+
.setEnabled(true)
616+
.build();
617+
618+
return updateSchedule(scheduleId, payload);
619+
}
620+
621+
/**
622+
* Disable the schedule.
623+
* @param scheduleId The schedule id.
624+
* @return The schedule information after updated.
625+
* @throws APIConnectionException
626+
* @throws APIRequestException
627+
*/
628+
public ScheduleResult disableSchedule(String scheduleId)
629+
throws APIConnectionException, APIRequestException {
630+
SchedulePayload payload = SchedulePayload.newBuilder()
631+
.setEnabled(false)
632+
.build();
633+
return updateSchedule(scheduleId, payload);
634+
}
635+
636+
/**
637+
* Update the trigger of the schedule.
638+
* @param scheduleId The schedule id.
639+
* @param trigger The new trigger.
640+
* @return The schedule information after updated.
641+
* @throws APIConnectionException
642+
* @throws APIRequestException
643+
*/
644+
public ScheduleResult updateScheduleTrigger(String scheduleId, TriggerPayload trigger)
645+
throws APIConnectionException, APIRequestException {
646+
SchedulePayload payload = SchedulePayload.newBuilder()
647+
.setTrigger(trigger)
648+
.build();
649+
650+
return updateSchedule(scheduleId, payload);
651+
}
652+
653+
/**
654+
* Update the push content of the schedule.
655+
* @param scheduleId The schedule id.
656+
* @param push The new push payload.
657+
* @return The schedule information after updated.
658+
* @throws APIConnectionException
659+
* @throws APIRequestException
660+
*/
661+
public ScheduleResult updateSchedulePush(String scheduleId, PushPayload push)
662+
throws APIConnectionException, APIRequestException {
663+
SchedulePayload payload = SchedulePayload.newBuilder()
664+
.setPush(push)
665+
.build();
666+
667+
return updateSchedule(scheduleId, payload);
668+
}
669+
588670
/**
589671
* Update a schedule by the id.
590672
* @param scheduleId The schedule id to update.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public enum Week {
55
TUE,
66
WED,
77
THU,
8-
FIR,
8+
FRI,
99
SAT,
1010
SUN
1111
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,26 @@ public TriggerPayload buildPeriodical() {
146146
Preconditions.checkArgument(TimeUtils.isTimeFormat(time), "The time format is incorrect.");
147147

148148
Preconditions.checkNotNull(time_unit, "The time_unit must not be null.");
149+
Preconditions.checkArgument(isTimeUnitOk(time_unit), "The time unit must be DAY, WEEK or MONTH.");
150+
149151
Preconditions.checkArgument(frequency > 0 && frequency < 101, "The frequency must be a int between 1 and 100.");
150152

151153
return new TriggerPayload(start, end, time, time_unit, frequency, point);
152154
}
153155

156+
private boolean isTimeUnitOk(TimeUnit timeUnit) {
157+
switch (timeUnit) {
158+
case HOUR:
159+
return false;
160+
case DAY:
161+
case WEEK:
162+
case MONTH:
163+
return true;
164+
default:
165+
return false;
166+
}
167+
}
168+
154169
}
155170

156171
}

0 commit comments

Comments
 (0)