55
66import cn .jpush .api .common .ClientConfig ;
77import cn .jpush .api .common .TimeUnit ;
8+ import cn .jpush .api .common .Week ;
89import cn .jpush .api .common .connection .HttpProxy ;
910import cn .jpush .api .common .resp .APIConnectionException ;
1011import cn .jpush .api .common .resp .APIRequestException ;
3031import cn .jpush .api .schedule .ScheduleResult ;
3132import cn .jpush .api .schedule .model .SchedulePayload ;
3233import 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
0 commit comments