Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions example/main/java/cn/jpush/api/examples/ScheduleExample.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
190 changes: 177 additions & 13 deletions src/main/java/cn/jpush/api/JPushClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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);
}

}

6 changes: 3 additions & 3 deletions src/main/java/cn/jpush/api/common/TimeUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public enum TimeUnit {

HOUR,
DAY,
MONTH

MONTH,
WEEK

}
11 changes: 11 additions & 0 deletions src/main/java/cn/jpush/api/common/Week.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cn.jpush.api.common;

public enum Week {
MON,
TUE,
WED,
THU,
FIR,
SAT,
SUN
}
10 changes: 4 additions & 6 deletions src/main/java/cn/jpush/api/schedule/model/TriggerPayload.java
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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));
Expand All @@ -77,10 +79,6 @@ public static enum Type {
single, periodical
}

public static enum TimeUnit {
day, week, month
}

public static class Builder{

private String start;
Expand Down
Loading