packagecom.bankhui.center.jpush;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importorg.apache.shiro.util.CollectionUtils;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcn.jpush.api.JPushClient;importcn.jpush.api.common.TimeUnit;importcn.jpush.api.common.Week;importcn.jpush.api.common.resp.APIConnectionException;importcn.jpush.api.common.resp.APIRequestException;importcn.jpush.api.push.model.Platform;importcn.jpush.api.push.model.PushPayload;importcn.jpush.api.push.model.audience.Audience;importcn.jpush.api.schedule.ScheduleListResult;importcn.jpush.api.schedule.ScheduleResult;importcn.jpush.api.schedule.model.SchedulePayload;importcn.jpush.api.schedule.model.TriggerPayload;public classJPushScheduleClient {protected static final Logger LOG = LoggerFactory.getLogger(JPushScheduleClient.class);private static final String appKey ="*********";private static final String masterSecret = "*******";/** 保存离线的时长。秒为单位。最多支持10天(864000秒)。
* 0 表示该消息不保存离线。即:用户在线马上发出,当前不在线用户将不会收到此消息。
* 此参数不设置则表示默认,默认为保存1天的离线消息(86400秒*/
private static int timeToLive = 60 * 60 * 24;public static voidmain(String[] args) {
List list = new ArrayList();
list.add("22");//testGetScheduleList();//testUpdateSchedule();
String scheduleId = "***************";
String time= "2017-03-07 09:55:00";
String msgTitle= "push schedule jpush,TEST\"\"";
String msgContent= "测试定时发送";
String sysMsgId= "26";
String type= "1";
String url= "https://www.baidu.com";//指定接收者的定时发送
scheduleId =createSingleSchedule(list,time,msgTitle,msgContent,sysMsgId,type,url);//全部用户的定时发送//scheduleId = createSingleSchedule(time,msgTitle,msgContent,sysMsgId,type,url);
testGetSchedule(scheduleId);//testDeleteSchedule(scheduleId);
}/*** 添加指定接收者定时发送消息的
*@paramaliases List 接收者极光id列表
*@paramtime 定时发送时间(yyyy-MM-dd HH:mm:ss)
*@parammsgTitle 标题
*@parammsgContent 内容
*@paramsysMsgId 系统保存的消息id
*@paramtype 跳转类型0不带链接跳转,1带链接跳转
*@paramurl 跳转url
*@return*@authorwxz
* @date 2017年3月7日*/
public static String createSingleSchedule(Listaliases,
String time, String msgTitle, String msgContent,
String sysMsgId, String type, String url) {if(CollectionUtils.isEmpty(aliases)){
LOG.info("aliases is empty");return null;
}
JPushClient jpushClient= newJPushClient(masterSecret, appKey, timeToLive);
String name= "schedule_"+time.replaceAll(" ", "").replaceAll(":", "").replaceAll("-", "");
Map extra = new HashMap();
extra.put("sysMsgId", sysMsgId);
extra.put("type", type);//0不带链接跳转,1带链接跳转
extra.put("url", url);//Message message = new cn.jpush.api.push.model.Message.Builder()//.setMsgContent(msgContent).addExtras(extra)//.build();//Audience audience = new cn.jpush.api.push.model.audience.Audience.Builder().build().alias(aliases);//初始化android消息通知
cn.jpush.api.push.model.notification.AndroidNotification androidNotification = newcn.jpush.api.push.model.notification.AndroidNotification.Builder().setAlert(msgContent).setTitle(msgTitle).addExtras(extra).build();//初始化ios消息通知
cn.jpush.api.push.model.notification.IosNotification iosNotification = newcn.jpush.api.push.model.notification.IosNotification.Builder().setAlert(msgContent).addExtras(extra).build();//初始化消息通知,将android和ios赋值
cn.jpush.api.push.model.notification.Notification notification = newcn.jpush.api.push.model.notification.Notification.Builder()
.addPlatformNotification(androidNotification)
.addPlatformNotification(iosNotification)
.build();//初始化push
PushPayload push = newcn.jpush.api.push.model.PushPayload.Builder()
.setPlatform(Platform.all())
.setAudience(Audience.alias(aliases))
.setNotification(notification)
.build();//PushPayload pucsh = PushPayload.alertAll("----test schedule example0000001111111.");
try{
ScheduleResult result=jpushClient.createSingleSchedule(name, time, push);
LOG.info("schedule result is " +result);returnresult.getSchedule_id();
}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());
}return null;
}/*** 添加所有用户定时发送消息的
*@paramtime 定时发送时间(yyyy-MM-dd HH:mm:ss)
*@parammsgTitle 标题
*@parammsgContent 内容
*@paramsysMsgId 系统保存的消息id
*@paramtype 跳转类型0不带链接跳转,1带链接跳转
*@paramurl 跳转url
*@return*@authorwxz
* @date 2017年3月7日*/
public staticString createSingleSchedule(String time, String msgTitle,
String msgContent, String sysMsgId, String type, String url) {
JPushClient jpushClient= newJPushClient(masterSecret, appKey, timeToLive);
String name= "schedule_"+time.replaceAll(" ", "").replaceAll(":", "").replaceAll("-", "");
Map extra = new HashMap();
extra.put("sysMsgId", sysMsgId);
extra.put("type", type);//0不带链接跳转,1带链接跳转
extra.put("url", url);//Message message = new cn.jpush.api.push.model.Message.Builder()//.setMsgContent(msgContent).addExtras(extra)//.build();//PushPayload push = new cn.jpush.api.push.model.PushPayload.Builder().setPlatform(Platform.all())//.setAudience(Audience.all())//.setMessage(message)////.setOptions(new cn.jpush.api.push.model.Options.Builder().setApnsProduction(true).build())//.build();//初始化android消息通知
cn.jpush.api.push.model.notification.AndroidNotification androidNotification = newcn.jpush.api.push.model.notification.AndroidNotification.Builder().setAlert(msgContent).setTitle(msgTitle).addExtras(extra).build();//初始化ios消息通知
cn.jpush.api.push.model.notification.IosNotification iosNotification = newcn.jpush.api.push.model.notification.IosNotification.Builder().setAlert(msgContent).addExtras(extra).build();//初始化消息通知,将android和ios赋值
cn.jpush.api.push.model.notification.Notification notification = newcn.jpush.api.push.model.notification.Notification.Builder()
.addPlatformNotification(androidNotification)
.addPlatformNotification(iosNotification)
.build();//初始化push
PushPayload push = newcn.jpush.api.push.model.PushPayload.Builder()
.setPlatform(Platform.all())
.setAudience(Audience.all())
.setNotification(notification)
.build();//PushPayload push = new cn.jpush.api.push.model.PushPayload.Builder()//.setPlatform(Platform.all())//.setAudience(Audience.all())//.setNotification(new cn.jpush.api.push.model.notification.Notification.Builder().addPlatformNotification(new cn.jpush.api.push.model.notification.AndroidNotification.Builder().setAlert(msgContent).setTitle(msgTitle).addExtras(extra).build())//.addPlatformNotification(new cn.jpush.api.push.model.notification.IosNotification.Builder().setAlert(msgContent).addExtras(extra).build())//.build())//.build();//PushPayload pucsh = PushPayload.alertAll("----test schedule example0000001111111.");
try{
ScheduleResult result=jpushClient.createSingleSchedule(name, time, push);
LOG.info("schedule result is " +result);returnresult.getSchedule_id();
}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());
}return null;
}private static voidtestCreateDailySchedule() {
JPushClient jPushClient= newJPushClient(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());
}
}private static voidtestCreateWeeklySchedule() {
JPushClient jPushClient= newJPushClient(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.FRI};
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());
}
}private static voidtestCreateMonthlySchedule() {
JPushClient jPushClient= newJPushClient(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());
}
}private static voidtestDeleteSchedule(String scheduleId) {//String scheduleId = "************************8";
JPushClient jpushClient = newJPushClient(masterSecret, appKey);try{
jpushClient.deleteSchedule(scheduleId);
}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());
}
}private static voidtestGetScheduleList() {int page = 1;
JPushClient jpushClient= newJPushClient(masterSecret, appKey);try{
ScheduleListResult list=jpushClient.getScheduleList(page);
LOG.info("total " +list.getTotal_count());for(ScheduleResult s : list.getSchedules()) {
LOG.info(s.toString());
}
}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());
}
}private static voidtestUpdateSchedule() {
String scheduleId= "*******************";
JPushClient jpushClient= newJPushClient(masterSecret, appKey);
String[] points={Week.MON.name(), Week.FRI.name()};
TriggerPayload trigger=TriggerPayload.newBuilder()
.setPeriodTime("2015-08-01 12:10:00", "2015-08-30 12:12:12", "15:00:00")
.setTimeFrequency(TimeUnit.WEEK,2, points)
.buildPeriodical();
SchedulePayload payload=SchedulePayload.newBuilder()
.setName("test_update_schedule")
.setEnabled(false)
.setTrigger(trigger)
.build();try{
jpushClient.updateSchedule(scheduleId, payload);
}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());
}
}private static voidtestGetSchedule(String scheduleId) {//String scheduleId = "************************";
JPushClient jpushClient = newJPushClient(masterSecret, appKey);try{
ScheduleResult result=jpushClient.getSchedule(scheduleId);
LOG.info("schedule " +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());
}
}/*** 组建push,若发送全部,则aliases传null
*@paramaliases List 接收者极光id列表
*@parammsgTitle 标题
*@parammsgContent 内容
*@paramsysMsgId 系统保存的消息id
*@paramtype 跳转类型0不带链接跳转,1带链接跳转
*@paramurl 跳转url
*@return*@authorwxz
* @date 2017年3月7日*/
private static PushPayload buildPush(Listaliases,String msgTitle, String msgContent,
String sysMsgId, String type, String url) {
Map extra = new HashMap();
extra.put("sysMsgId", sysMsgId);
extra.put("type", type);//0不带链接跳转,1带链接跳转
extra.put("url", url);//初始化android消息通知
cn.jpush.api.push.model.notification.AndroidNotification androidNotification = newcn.jpush.api.push.model.notification.AndroidNotification.Builder().setAlert(msgContent).setTitle(msgTitle).addExtras(extra).build();//初始化ios消息通知
cn.jpush.api.push.model.notification.IosNotification iosNotification = newcn.jpush.api.push.model.notification.IosNotification.Builder().setAlert(msgContent).addExtras(extra).build();//初始化消息通知,将android和ios赋值
cn.jpush.api.push.model.notification.Notification notification = newcn.jpush.api.push.model.notification.Notification.Builder()
.addPlatformNotification(androidNotification)
.addPlatformNotification(iosNotification)
.build();//初始化push
PushPayload push = newcn.jpush.api.push.model.PushPayload.Builder()
.setPlatform(Platform.all())
.setAudience(CollectionUtils.isEmpty(aliases)?Audience.all():Audience.alias(aliases))
.setNotification(notification)
.build();returnpush;
}
}
这个博客演示了如何使用JPushClient Java SDK来创建和管理定时任务,包括单个接收者和全体用户的定时消息发送。示例代码详细展示了如何设置消息标题、内容、附加信息以及定时发送的时间,并提供了创建、更新、获取和删除定时任务的方法。
1599

被折叠的 条评论
为什么被折叠?



