JPushClient.java 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. package com.goafanti.message;
  2. import java.util.Map;
  3. import java.util.Set;
  4. import com.goafanti.message.device.AliasDeviceListResult;
  5. import com.goafanti.message.device.DeviceClient;
  6. import com.goafanti.message.device.OnlineStatus;
  7. import com.goafanti.message.device.TagAliasResult;
  8. import com.goafanti.message.device.TagListResult;
  9. import com.goafanti.message.push.CIDResult;
  10. import com.goafanti.message.push.PushClient;
  11. import com.goafanti.message.push.PushResult;
  12. import com.goafanti.message.push.model.Message;
  13. import com.goafanti.message.push.model.Platform;
  14. import com.goafanti.message.push.model.PushPayload;
  15. import com.goafanti.message.push.model.SMS;
  16. import com.goafanti.message.push.model.audience.Audience;
  17. import com.goafanti.message.push.model.notification.IosAlert;
  18. import com.goafanti.message.push.model.notification.Notification;
  19. import com.goafanti.message.report.MessageStatus;
  20. import com.goafanti.message.report.MessagesResult;
  21. import com.goafanti.message.report.ReceivedsResult;
  22. import com.goafanti.message.report.ReportClient;
  23. import com.goafanti.message.report.UsersResult;
  24. import com.goafanti.message.report.model.CheckMessagePayload;
  25. import com.goafanti.message.schedule.ScheduleClient;
  26. import com.goafanti.message.schedule.ScheduleListResult;
  27. import com.goafanti.message.schedule.ScheduleResult;
  28. import com.goafanti.message.schedule.model.SchedulePayload;
  29. import com.goafanti.message.schedule.model.TriggerPayload;
  30. import com.google.gson.JsonObject;
  31. import cn.jiguang.common.ClientConfig;
  32. import cn.jiguang.common.TimeUnit;
  33. import cn.jiguang.common.Week;
  34. import cn.jiguang.common.utils.Preconditions;
  35. import cn.jiguang.common.connection.HttpProxy;
  36. import cn.jiguang.common.resp.APIConnectionException;
  37. import cn.jiguang.common.resp.APIRequestException;
  38. import cn.jiguang.common.resp.BooleanResult;
  39. import cn.jiguang.common.resp.DefaultResult;
  40. /**
  41. * The global entrance of JPush API library.
  42. */
  43. public class JPushClient {
  44. private final PushClient _pushClient;
  45. private final ReportClient _reportClient;
  46. private final DeviceClient _deviceClient;
  47. private final ScheduleClient _scheduleClient;
  48. /**
  49. * Create a JPush Client.
  50. *
  51. * @param masterSecret API access secret of the appKey.
  52. * @param appKey The KEY of one application on JPush.
  53. */
  54. public JPushClient(String masterSecret, String appKey) {
  55. _pushClient = new PushClient(masterSecret, appKey);
  56. _reportClient = new ReportClient(masterSecret, appKey);
  57. _deviceClient = new DeviceClient(masterSecret, appKey);
  58. _scheduleClient = new ScheduleClient(masterSecret, appKey);
  59. }
  60. /**
  61. * Create a JPush Client by custom Client configuration.
  62. *
  63. * @param masterSecret API access secret of the appKey.
  64. * @param appKey The KEY of one application on JPush.
  65. * @param proxy The proxy, if there is no proxy, should be null.
  66. * @param conf The client configuration. Can use ClientConfig.getInstance() as default.
  67. */
  68. public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) {
  69. _pushClient = new PushClient(masterSecret, appKey, proxy, conf);
  70. _reportClient = new ReportClient(masterSecret, appKey, proxy, conf);
  71. _deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf);
  72. _scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf);
  73. }
  74. /**
  75. * This will be removed in the future. Please use ClientConfig{jiguang-common cn.jiguang.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
  76. * @param masterSecret API access secret of the appKey.
  77. * @param appKey The KEY of one application on JPush.
  78. * @param maxRetryTimes The max retry times.
  79. */
  80. @Deprecated
  81. public JPushClient(String masterSecret, String appKey, int maxRetryTimes) {
  82. _pushClient = new PushClient(masterSecret, appKey, maxRetryTimes);
  83. _reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes);
  84. _deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes);
  85. _scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes);
  86. }
  87. /**
  88. * This will be removed in the future. Please use ClientConfig{jiguang-common cn.jiguang.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
  89. * @param masterSecret API access secret of the appKey.
  90. * @param appKey The KEY of one application on JPush.
  91. * @param maxRetryTimes The max retry times.
  92. * @param proxy The proxy, if there is no proxy, should be null.
  93. */
  94. @Deprecated
  95. public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
  96. _pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy);
  97. _reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy);
  98. _deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy);
  99. _scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes, proxy);
  100. }
  101. /**
  102. * Create a JPush Client by custom Client configuration.
  103. *
  104. * If you are using JPush privacy cloud, maybe this constructor is what you needed.
  105. * This will be removed in the future. Please use ClientConfig{jiguang-common cn.jiguang.common.ClientConfig#setMaxRetryTimes} instead of this constructor.
  106. *
  107. * @param masterSecret API access secret of the appKey.
  108. * @param appKey The KEY of one application on JPush.
  109. * @param maxRetryTimes Client request retry times.
  110. * @param proxy The proxy, if there is no proxy, should be null.
  111. * @param conf The client configuration. Can use ClientConfig.getInstance() as default.
  112. */
  113. @Deprecated
  114. public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) {
  115. conf.setMaxRetryTimes(maxRetryTimes);
  116. _pushClient = new PushClient(masterSecret, appKey, proxy, conf);
  117. _reportClient = new ReportClient(masterSecret, appKey, proxy, conf);
  118. _deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf);
  119. _scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf);
  120. }
  121. /**
  122. * Create a JPush Client by custom Client configuration with global settings.
  123. *
  124. * If you are using JPush privacy cloud, and you want different settings from default globally,
  125. * maybe this constructor is what you needed.
  126. * This will be removed in the future. Please use ClientConfig{jiguang-common cn.jiguang.common.ClientConfig#setGlobalPushSetting} instead of this constructor.
  127. *
  128. * @param masterSecret API access secret of the appKey.
  129. * @param appKey The KEY of one application on JPush.
  130. * @param maxRetryTimes Client request retry times.
  131. * @param proxy The proxy, if there is no proxy, should be null.
  132. * @param conf The client configuration. Can use ClientConfig.getInstance() as default.
  133. * @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
  134. * @param timeToLive Global time_to_live setting. It will override PushPayload Options.
  135. */
  136. @Deprecated
  137. public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf,
  138. boolean apnsProduction, long timeToLive) {
  139. conf.setMaxRetryTimes(maxRetryTimes);
  140. conf.setApnsProduction(apnsProduction);
  141. conf.setTimeToLive(timeToLive);
  142. _pushClient = new PushClient(masterSecret, appKey, proxy, conf);
  143. _reportClient = new ReportClient(masterSecret, appKey, proxy, conf);
  144. _deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf);
  145. _scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf);
  146. }
  147. /**
  148. * Create a JPush Client with global settings.
  149. *
  150. * If you want different settings from default globally, this constructor is what you needed.
  151. * This will be removed in the future. Please use ClientConfig{jiguang-common cn.jiguang.common.ClientConfig#setGlobalPushSetting} instead of this constructor.
  152. *
  153. * @param masterSecret API access secret of the appKey.
  154. * @param appKey The KEY of one application on JPush.
  155. * @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
  156. * @param timeToLive Global time_to_live setting. It will override PushPayload Options.
  157. */
  158. @Deprecated
  159. public JPushClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) {
  160. ClientConfig conf = ClientConfig.getInstance();
  161. conf.setApnsProduction(apnsProduction);
  162. conf.setTimeToLive(timeToLive);
  163. _pushClient = new PushClient(masterSecret, appKey);
  164. _reportClient = new ReportClient(masterSecret, appKey);
  165. _deviceClient = new DeviceClient(masterSecret, appKey);
  166. _scheduleClient = new ScheduleClient(masterSecret, appKey);
  167. }
  168. public PushClient getPushClient() {
  169. return _pushClient;
  170. }
  171. // ----------------------------- Push API
  172. /**
  173. * Send a push with PushPayload object.
  174. *
  175. * @param pushPayload payload object of a push.
  176. * @return PushResult The result object of a Push. Can be printed to a JSON.
  177. * @throws APIConnectionException if a remote or network exception occurs.
  178. * @throws APIRequestException if a request exception occurs.
  179. */
  180. public PushResult sendPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
  181. return _pushClient.sendPush(pushPayload);
  182. }
  183. /**
  184. * Send a push with JSON string.
  185. *
  186. * You can send a push JSON string directly with this method.
  187. *
  188. * Attention: globally settings cannot be affect this type of Push.
  189. *
  190. * @param payloadString payload of a push.
  191. * @return PushResult. Can be printed to a JSON.
  192. * @throws APIConnectionException if a remote or network exception occurs.
  193. * @throws APIRequestException if a request exception occurs.
  194. */
  195. public PushResult sendPush(String payloadString) throws APIConnectionException, APIRequestException {
  196. return _pushClient.sendPush(payloadString);
  197. }
  198. /**
  199. * Validate a push action, but do NOT send it actually.
  200. *
  201. * @param payload payload of a push.
  202. * @return PushResult. Can be printed to a JSON.
  203. * @throws APIConnectionException if a remote or network exception occurs.
  204. * @throws APIRequestException if a request exception occurs.
  205. */
  206. public PushResult sendPushValidate(PushPayload payload) throws APIConnectionException, APIRequestException {
  207. return _pushClient.sendPushValidate(payload);
  208. }
  209. public PushResult sendPushValidate(String payloadString) throws APIConnectionException, APIRequestException {
  210. return _pushClient.sendPushValidate(payloadString);
  211. }
  212. /**
  213. * Get cid list, the data form of cid is appKey-uuid.
  214. * @param count the count of cid list, from 1 to 1000. default is 1.
  215. * @param type default is push, option: schedule
  216. * @return CIDResult, an array of cid
  217. * @throws APIConnectionException connect exception
  218. * @throws APIRequestException request exception
  219. */
  220. public CIDResult getCidList(int count, String type) throws APIConnectionException, APIRequestException {
  221. return _pushClient.getCidList(count, type);
  222. }
  223. // ------------------------------- Report API
  224. /**
  225. * Get received report.
  226. *
  227. * @param msgIds 100 msgids to batch getting is supported.
  228. * @return ReceivedResult. Can be printed to JSON.
  229. * @throws APIConnectionException if a remote or network exception occurs.
  230. * @throws APIRequestException if a request exception occurs.
  231. */
  232. public ReceivedsResult getReportReceiveds(String msgIds) throws APIConnectionException, APIRequestException {
  233. return _reportClient.getReceiveds(msgIds);
  234. }
  235. public UsersResult getReportUsers(TimeUnit timeUnit, String start, int duration) throws APIConnectionException, APIRequestException {
  236. return _reportClient.getUsers(timeUnit, start, duration);
  237. }
  238. public MessagesResult getReportMessages(String msgIds) throws APIConnectionException, APIRequestException {
  239. return _reportClient.getMessages(msgIds);
  240. }
  241. public Map<String, MessageStatus> getMessageStatus(CheckMessagePayload payload)
  242. throws APIConnectionException, APIRequestException {
  243. return _reportClient.getMessagesStatus(payload);
  244. }
  245. // ------------------------------ Shortcuts - notification
  246. public PushResult sendNotificationAll(String alert) throws APIConnectionException, APIRequestException {
  247. PushPayload payload = PushPayload.alertAll(alert);
  248. return _pushClient.sendPush(payload);
  249. }
  250. /**
  251. * Send a notification to all.
  252. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  253. *
  254. * @param alert The notification content.
  255. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  256. * @return push result
  257. * @throws APIConnectionException if a remote or network exception occurs.
  258. * @throws APIRequestException if a request exception occurs.
  259. */
  260. public PushResult sendNotificationAll(String alert, SMS sms) throws APIConnectionException, APIRequestException {
  261. PushPayload payload = PushPayload.alertAll(alert, sms);
  262. return _pushClient.sendPush(payload);
  263. }
  264. public PushResult sendAndroidNotificationWithAlias(String title, String alert,
  265. Map<String, String> extras, String... alias)
  266. throws APIConnectionException, APIRequestException {
  267. PushPayload payload = PushPayload.newBuilder()
  268. .setPlatform(Platform.android())
  269. .setAudience(Audience.alias(alias))
  270. .setNotification(Notification.android(alert, title, extras))
  271. .build();
  272. return _pushClient.sendPush(payload);
  273. }
  274. /**
  275. * Send a notification to Android with alias.
  276. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  277. *
  278. * @param title The notification title.
  279. * @param alert The notification content.
  280. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  281. * @param extras The extra parameter.
  282. * @param alias The users' alias.
  283. * @return push result.
  284. * @throws APIConnectionException if a remote or network exception occurs.
  285. * @throws APIRequestException if a request exception occurs.
  286. */
  287. public PushResult sendAndroidNotificationWithAlias(String title, String alert, SMS sms,
  288. Map<String, String> extras, String... alias)
  289. throws APIConnectionException, APIRequestException {
  290. PushPayload payload = PushPayload.newBuilder()
  291. .setPlatform(Platform.android())
  292. .setAudience(Audience.alias(alias))
  293. .setNotification(Notification.android(alert, title, extras))
  294. .setSMS(sms)
  295. .build();
  296. return _pushClient.sendPush(payload);
  297. }
  298. public PushResult sendAndroidNotificationWithRegistrationID(String title, String alert,
  299. Map<String, String> extras, String... registrationID)
  300. throws APIConnectionException, APIRequestException {
  301. PushPayload payload = PushPayload.newBuilder()
  302. .setPlatform(Platform.android())
  303. .setAudience(Audience.registrationId(registrationID))
  304. .setNotification(Notification.android(alert, title, extras))
  305. .build();
  306. return _pushClient.sendPush(payload);
  307. }
  308. /**
  309. * Send a notification to Android with RegistrationID.
  310. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  311. *
  312. * @param title The notification title.
  313. * @param alert The notification content.
  314. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  315. * @param extras The extra parameter.
  316. * @param registrationID The registration id generated by JPush.
  317. * @return push result.
  318. * @throws APIConnectionException if a remote or network exception occurs.
  319. * @throws APIRequestException if a request exception occurs.
  320. */
  321. public PushResult sendAndroidNotificationWithRegistrationID(String title, String alert, SMS sms,
  322. Map<String, String> extras, String... registrationID)
  323. throws APIConnectionException, APIRequestException {
  324. PushPayload payload = PushPayload.newBuilder()
  325. .setPlatform(Platform.android())
  326. .setAudience(Audience.registrationId(registrationID))
  327. .setNotification(Notification.android(alert, title, extras))
  328. .setSMS(sms)
  329. .build();
  330. return _pushClient.sendPush(payload);
  331. }
  332. public PushResult sendIosNotificationWithAlias(String alert,
  333. Map<String, String> extras, String... alias)
  334. throws APIConnectionException, APIRequestException {
  335. PushPayload payload = PushPayload.newBuilder()
  336. .setPlatform(Platform.ios())
  337. .setAudience(Audience.alias(alias))
  338. .setNotification(Notification.ios(alert, extras))
  339. .build();
  340. return _pushClient.sendPush(payload);
  341. }
  342. /**
  343. * Send a notification to iOS with alias.
  344. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  345. * @param alert The notification content.
  346. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  347. * @param extras The extra parameter.
  348. * @param alias The users' alias.
  349. * @return push result.
  350. * @throws APIConnectionException if a remote or network exception occurs.
  351. * @throws APIRequestException if a request exception occurs.
  352. */
  353. public PushResult sendIosNotificationWithAlias(String alert, SMS sms,
  354. Map<String, String> extras, String... alias)
  355. throws APIConnectionException, APIRequestException {
  356. PushPayload payload = PushPayload.newBuilder()
  357. .setPlatform(Platform.ios())
  358. .setAudience(Audience.alias(alias))
  359. .setNotification(Notification.ios(alert, extras))
  360. .setSMS(sms)
  361. .build();
  362. return _pushClient.sendPush(payload);
  363. }
  364. /**
  365. * Send an iOS notification with alias.
  366. * If you want to send alert as a Json object, maybe this method is what you needed.
  367. *
  368. * @param alert The wrapper of APNs alert.
  369. * @param extras The extra params.
  370. * @param alias The alias list.
  371. * @return push result.
  372. * @throws APIConnectionException if a remote or network exception occurs.
  373. * @throws APIRequestException if a request exception occurs.
  374. */
  375. public PushResult sendIosNotificationWithAlias(IosAlert alert,
  376. Map<String, String> extras, String... alias)
  377. throws APIConnectionException, APIRequestException {
  378. PushPayload payload = PushPayload.newBuilder()
  379. .setPlatform(Platform.ios())
  380. .setAudience(Audience.alias(alias))
  381. .setNotification(Notification.ios(alert, extras))
  382. .build();
  383. return _pushClient.sendPush(payload);
  384. }
  385. /**
  386. * Send an iOS notification with alias.
  387. * If you want to send alert as a Json object, maybe this method is what you needed.
  388. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  389. *
  390. * @param alert The wrapper of APNs alert.
  391. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  392. * @param extras The extra params.
  393. * @param alias The alias list.
  394. * @return push result.
  395. * @throws APIConnectionException if a remote or network exception occurs.
  396. * @throws APIRequestException if a request exception occurs.
  397. */
  398. public PushResult sendIosNotificationWithAlias(IosAlert alert, SMS sms,
  399. Map<String, String> extras, String... alias)
  400. throws APIConnectionException, APIRequestException {
  401. PushPayload payload = PushPayload.newBuilder()
  402. .setPlatform(Platform.ios())
  403. .setAudience(Audience.alias(alias))
  404. .setNotification(Notification.ios(alert, extras))
  405. .setSMS(sms)
  406. .build();
  407. return _pushClient.sendPush(payload);
  408. }
  409. /**
  410. * Send an iOS notification with alias.
  411. * If you want to send alert as a Json object, maybe this method is what you needed.
  412. *
  413. * @param alert The JSON object of APNs alert.
  414. * @param extras The extra params.
  415. * @param alias The alias list.
  416. * @return push result.
  417. * @throws APIConnectionException if a remote or network exception occurs.
  418. * @throws APIRequestException if a request exception occurs.
  419. */
  420. public PushResult sendIosNotificationWithAlias(JsonObject alert,
  421. Map<String, String> extras, String... alias)
  422. throws APIConnectionException, APIRequestException {
  423. PushPayload payload = PushPayload.newBuilder()
  424. .setPlatform(Platform.ios())
  425. .setAudience(Audience.alias(alias))
  426. .setNotification(Notification.ios(alert, extras))
  427. .build();
  428. return _pushClient.sendPush(payload);
  429. }
  430. /**
  431. * Send an iOS notification with alias.
  432. * If you want to send alert as a Json object, maybe this method is what you needed.
  433. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  434. *
  435. * @param alert The JSON object of APNs alert.
  436. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  437. * @param extras The extra params.
  438. * @param alias The alias list.
  439. * @return push result.
  440. * @throws APIConnectionException if a remote or network exception occurs.
  441. * @throws APIRequestException if a request exception occurs.
  442. */
  443. public PushResult sendIosNotificationWithAlias(JsonObject alert, SMS sms,
  444. Map<String, String> extras, String... alias)
  445. throws APIConnectionException, APIRequestException {
  446. PushPayload payload = PushPayload.newBuilder()
  447. .setPlatform(Platform.ios())
  448. .setAudience(Audience.alias(alias))
  449. .setNotification(Notification.ios(alert, extras))
  450. .setSMS(sms)
  451. .build();
  452. return _pushClient.sendPush(payload);
  453. }
  454. public PushResult sendIosNotificationWithRegistrationID(String alert,
  455. Map<String, String> extras, String... registrationID)
  456. throws APIConnectionException, APIRequestException {
  457. PushPayload payload = PushPayload.newBuilder()
  458. .setPlatform(Platform.ios())
  459. .setAudience(Audience.registrationId(registrationID))
  460. .setNotification(Notification.ios(alert, extras))
  461. .build();
  462. return _pushClient.sendPush(payload);
  463. }
  464. /**
  465. * Send an iOS notification with registrationIds.
  466. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  467. *
  468. * @param alert The notification content.
  469. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  470. * @param extras The extra params.
  471. * @param registrationID The alias list.
  472. * @return push result.
  473. * @throws APIConnectionException if a remote or network exception occurs.
  474. * @throws APIRequestException if a request exception occurs.
  475. */
  476. public PushResult sendIosNotificationWithRegistrationID(String alert, SMS sms,
  477. Map<String, String> extras, String... registrationID)
  478. throws APIConnectionException, APIRequestException {
  479. PushPayload payload = PushPayload.newBuilder()
  480. .setPlatform(Platform.ios())
  481. .setAudience(Audience.registrationId(registrationID))
  482. .setNotification(Notification.ios(alert, extras))
  483. .setSMS(sms)
  484. .build();
  485. return _pushClient.sendPush(payload);
  486. }
  487. /**
  488. * Send an iOS notification with registrationIds.
  489. * If you want to send alert as a Json object, maybe this method is what you needed.
  490. *
  491. * @param alert The wrapper of APNs alert.
  492. * @param extras The extra params.
  493. * @param registrationID The registration ids.
  494. * @return push result.
  495. * @throws APIConnectionException if a remote or network exception occurs.
  496. * @throws APIRequestException if a request exception occurs.
  497. */
  498. public PushResult sendIosNotificationWithRegistrationID(IosAlert alert,
  499. Map<String, String> extras, String... registrationID)
  500. throws APIConnectionException, APIRequestException {
  501. PushPayload payload = PushPayload.newBuilder()
  502. .setPlatform(Platform.ios())
  503. .setAudience(Audience.registrationId(registrationID))
  504. .setNotification(Notification.ios(alert, extras))
  505. .build();
  506. return _pushClient.sendPush(payload);
  507. }
  508. /**
  509. * Send an iOS notification with registrationIds.
  510. * If you want to send alert as a Json object, maybe this method is what you needed.
  511. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  512. *
  513. * @param alert The wrapper of APNs alert.
  514. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  515. * @param extras The extra params.
  516. * @param registrationID The registration ids.
  517. * @return push result.
  518. * @throws APIConnectionException if a remote or network exception occurs.
  519. * @throws APIRequestException if a request exception occurs.
  520. */
  521. public PushResult sendIosNotificationWithRegistrationID(IosAlert alert, SMS sms,
  522. Map<String, String> extras, String... registrationID)
  523. throws APIConnectionException, APIRequestException {
  524. PushPayload payload = PushPayload.newBuilder()
  525. .setPlatform(Platform.ios())
  526. .setAudience(Audience.registrationId(registrationID))
  527. .setNotification(Notification.ios(alert, extras))
  528. .setSMS(sms)
  529. .build();
  530. return _pushClient.sendPush(payload);
  531. }
  532. /**
  533. * Send an iOS notification with registrationIds.
  534. * If you want to send alert as a Json object, maybe this method is what you needed.
  535. *
  536. * @param alert The wrapper of APNs alert.
  537. * @param extras The extra params.
  538. * @param registrationID The registration ids.
  539. * @return push result.
  540. * @throws APIConnectionException if a remote or network exception occurs.
  541. * @throws APIRequestException if a request exception occurs.
  542. */
  543. public PushResult sendIosNotificationWithRegistrationID(JsonObject alert,
  544. Map<String, String> extras, String... registrationID)
  545. throws APIConnectionException, APIRequestException {
  546. PushPayload payload = PushPayload.newBuilder()
  547. .setPlatform(Platform.ios())
  548. .setAudience(Audience.registrationId(registrationID))
  549. .setNotification(Notification.ios(alert, extras))
  550. .build();
  551. return _pushClient.sendPush(payload);
  552. }
  553. /**
  554. * Send an iOS notification with registrationIds.
  555. * If you want to send alert as a Json object, maybe this method is what you needed.
  556. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  557. *
  558. * @param alert The JSON object of APNs alert.
  559. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  560. * @param extras The extra params.
  561. * @param registrationID The registration ids.
  562. * @return push result.
  563. * @throws APIConnectionException if a remote or network exception occurs.
  564. * @throws APIRequestException if a request exception occurs.
  565. */
  566. public PushResult sendIosNotificationWithRegistrationID(JsonObject alert, SMS sms,
  567. Map<String, String> extras, String... registrationID)
  568. throws APIConnectionException, APIRequestException {
  569. PushPayload payload = PushPayload.newBuilder()
  570. .setPlatform(Platform.ios())
  571. .setAudience(Audience.registrationId(registrationID))
  572. .setNotification(Notification.ios(alert, extras))
  573. .setSMS(sms)
  574. .build();
  575. return _pushClient.sendPush(payload);
  576. }
  577. // ---------------------- shortcuts - message
  578. public PushResult sendMessageAll(String msgContent) throws APIConnectionException, APIRequestException {
  579. PushPayload payload = PushPayload.messageAll(msgContent);
  580. return _pushClient.sendPush(payload);
  581. }
  582. /**
  583. * Send a message to all
  584. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  585. *
  586. * @param msgContent The message content.
  587. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  588. * @return push result.
  589. * @throws APIConnectionException if a remote or network exception occurs.
  590. * @throws APIRequestException if a request exception occurs.
  591. */
  592. public PushResult sendMessageAll(String msgContent, SMS sms) throws APIConnectionException, APIRequestException {
  593. PushPayload payload = PushPayload.messageAll(msgContent, sms);
  594. return _pushClient.sendPush(payload);
  595. }
  596. public PushResult sendAndroidMessageWithAlias(String title, String msgContent, String... alias)
  597. throws APIConnectionException, APIRequestException {
  598. PushPayload payload = PushPayload.newBuilder()
  599. .setPlatform(Platform.android())
  600. .setAudience(Audience.alias(alias))
  601. .setMessage(Message.newBuilder()
  602. .setTitle(title)
  603. .setMsgContent(msgContent)
  604. .build())
  605. .build();
  606. return _pushClient.sendPush(payload);
  607. }
  608. /**
  609. * Send an Android message with alias.
  610. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  611. *
  612. * @param title The message title.
  613. * @param msgContent The message content.
  614. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  615. * @param alias The alias list.
  616. * @return push result.
  617. * @throws APIConnectionException if a remote or network exception occurs.
  618. * @throws APIRequestException if a request exception occurs.
  619. */
  620. public PushResult sendAndroidMessageWithAlias(String title, String msgContent, SMS sms, String... alias)
  621. throws APIConnectionException, APIRequestException {
  622. PushPayload payload = PushPayload.newBuilder()
  623. .setPlatform(Platform.android())
  624. .setAudience(Audience.alias(alias))
  625. .setMessage(Message.newBuilder()
  626. .setTitle(title)
  627. .setMsgContent(msgContent)
  628. .build())
  629. .setSMS(sms)
  630. .build();
  631. return _pushClient.sendPush(payload);
  632. }
  633. public PushResult sendAndroidMessageWithRegistrationID(String title, String msgContent, String... registrationID)
  634. throws APIConnectionException, APIRequestException {
  635. PushPayload payload = PushPayload.newBuilder()
  636. .setPlatform(Platform.android())
  637. .setAudience(Audience.registrationId(registrationID))
  638. .setMessage(Message.newBuilder()
  639. .setTitle(title)
  640. .setMsgContent(msgContent)
  641. .build())
  642. .build();
  643. return _pushClient.sendPush(payload);
  644. }
  645. /**
  646. * Send an Android message with registration id.
  647. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  648. *
  649. * @param title The message title.
  650. * @param msgContent The message content.
  651. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  652. * @param registrationID The registration id list.
  653. * @return push result.
  654. * @throws APIConnectionException if a remote or network exception occurs.
  655. * @throws APIRequestException if a request exception occurs.
  656. */
  657. public PushResult sendAndroidMessageWithRegistrationID(String title, String msgContent, SMS sms, String... registrationID)
  658. throws APIConnectionException, APIRequestException {
  659. PushPayload payload = PushPayload.newBuilder()
  660. .setPlatform(Platform.android())
  661. .setAudience(Audience.registrationId(registrationID))
  662. .setMessage(Message.newBuilder()
  663. .setTitle(title)
  664. .setMsgContent(msgContent)
  665. .build())
  666. .setSMS(sms)
  667. .build();
  668. return _pushClient.sendPush(payload);
  669. }
  670. public PushResult sendIosMessageWithAlias(String title, String msgContent, String... alias)
  671. throws APIConnectionException, APIRequestException {
  672. PushPayload payload = PushPayload.newBuilder()
  673. .setPlatform(Platform.ios())
  674. .setAudience(Audience.alias(alias))
  675. .setMessage(Message.newBuilder()
  676. .setTitle(title)
  677. .setMsgContent(msgContent)
  678. .build())
  679. .build();
  680. return _pushClient.sendPush(payload);
  681. }
  682. /**
  683. * Send an iOS message with alias.
  684. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  685. *
  686. * @param title The message title.
  687. * @param msgContent The message content.
  688. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  689. * @param alias The alias list.
  690. * @return push result.
  691. * @throws APIConnectionException if a remote or network exception occurs.
  692. * @throws APIRequestException if a request exception occurs.
  693. */
  694. public PushResult sendIosMessageWithAlias(String title, String msgContent, SMS sms, String... alias)
  695. throws APIConnectionException, APIRequestException {
  696. PushPayload payload = PushPayload.newBuilder()
  697. .setPlatform(Platform.ios())
  698. .setAudience(Audience.alias(alias))
  699. .setMessage(Message.newBuilder()
  700. .setTitle(title)
  701. .setMsgContent(msgContent)
  702. .build())
  703. .setSMS(sms)
  704. .build();
  705. return _pushClient.sendPush(payload);
  706. }
  707. public PushResult sendIosMessageWithRegistrationID(String title, String msgContent, String... registrationID)
  708. throws APIConnectionException, APIRequestException {
  709. PushPayload payload = PushPayload.newBuilder()
  710. .setPlatform(Platform.ios())
  711. .setAudience(Audience.registrationId(registrationID))
  712. .setMessage(Message.newBuilder()
  713. .setTitle(title)
  714. .setMsgContent(msgContent)
  715. .build())
  716. .build();
  717. return _pushClient.sendPush(payload);
  718. }
  719. /**
  720. * Send an iOS message with registration id.
  721. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  722. *
  723. * @param title The message title.
  724. * @param msgContent The message content.
  725. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  726. * @param registrationID The registrationIds generated by JPush.
  727. * @return push result.
  728. * @throws APIConnectionException if a remote or network exception occurs.
  729. * @throws APIRequestException if a request exception occurs.
  730. */
  731. public PushResult sendIosMessageWithRegistrationID(String title, String msgContent, SMS sms, String... registrationID)
  732. throws APIConnectionException, APIRequestException {
  733. PushPayload payload = PushPayload.newBuilder()
  734. .setPlatform(Platform.ios())
  735. .setAudience(Audience.registrationId(registrationID))
  736. .setMessage(Message.newBuilder()
  737. .setTitle(title)
  738. .setMsgContent(msgContent)
  739. .build())
  740. .setSMS(sms)
  741. .build();
  742. return _pushClient.sendPush(payload);
  743. }
  744. public PushResult sendMessageWithRegistrationID(String title, String msgContent, String... registrationID)
  745. throws APIConnectionException, APIRequestException {
  746. PushPayload payload = PushPayload.newBuilder()
  747. .setPlatform(Platform.all())
  748. .setAudience(Audience.registrationId(registrationID))
  749. .setMessage(Message.newBuilder()
  750. .setTitle(title)
  751. .setMsgContent(msgContent)
  752. .build())
  753. .build();
  754. return _pushClient.sendPush(payload);
  755. }
  756. /**
  757. * Send a message with registrationIds.
  758. * If it doesn't received within the delay time,JPush will send a SMS to the corresponding users.
  759. *
  760. * @param title The message title.
  761. * @param msgContent The message content.
  762. * @param sms The SMS content and delay time. If null, sms doesn't work, no effect on Push feature.
  763. * @param registrationID The registrationIds generated by JPush.
  764. * @return push result.
  765. * @throws APIConnectionException if a remote or network exception occurs.
  766. * @throws APIRequestException if a request exception occurs.
  767. */
  768. public PushResult sendMessageWithRegistrationID(String title, String msgContent, SMS sms, String... registrationID)
  769. throws APIConnectionException, APIRequestException {
  770. PushPayload payload = PushPayload.newBuilder()
  771. .setPlatform(Platform.all())
  772. .setAudience(Audience.registrationId(registrationID))
  773. .setMessage(Message.newBuilder()
  774. .setTitle(title)
  775. .setMsgContent(msgContent)
  776. .build())
  777. .setSMS(sms)
  778. .build();
  779. return _pushClient.sendPush(payload);
  780. }
  781. // ----------------------- Device
  782. public TagAliasResult getDeviceTagAlias(String registrationId)
  783. throws APIConnectionException, APIRequestException {
  784. return _deviceClient.getDeviceTagAlias(registrationId);
  785. }
  786. public DefaultResult updateDeviceTagAlias(String registrationId, boolean clearAlias, boolean clearTag)
  787. throws APIConnectionException, APIRequestException {
  788. return _deviceClient.updateDeviceTagAlias(registrationId, clearAlias, clearTag);
  789. }
  790. public DefaultResult updateDeviceTagAlias(String registrationId, String alias,
  791. Set<String> tagsToAdd, Set<String> tagsToRemove)
  792. throws APIConnectionException, APIRequestException {
  793. return _deviceClient.updateDeviceTagAlias(registrationId, alias, tagsToAdd, tagsToRemove);
  794. }
  795. public TagListResult getTagList()
  796. throws APIConnectionException, APIRequestException {
  797. return _deviceClient.getTagList();
  798. }
  799. public BooleanResult isDeviceInTag(String theTag, String registrationID)
  800. throws APIConnectionException, APIRequestException {
  801. return _deviceClient.isDeviceInTag(theTag, registrationID);
  802. }
  803. public DefaultResult addRemoveDevicesFromTag(String theTag,
  804. Set<String> toAddUsers, Set<String> toRemoveUsers)
  805. throws APIConnectionException, APIRequestException {
  806. return _deviceClient.addRemoveDevicesFromTag(theTag, toAddUsers,
  807. toRemoveUsers);
  808. }
  809. public DefaultResult deleteTag(String theTag, String platform)
  810. throws APIConnectionException, APIRequestException {
  811. return _deviceClient.deleteTag(theTag, platform);
  812. }
  813. public AliasDeviceListResult getAliasDeviceList(String alias,
  814. String platform) throws APIConnectionException, APIRequestException {
  815. return _deviceClient.getAliasDeviceList(alias, platform);
  816. }
  817. public DefaultResult deleteAlias(String alias, String platform)
  818. throws APIConnectionException, APIRequestException {
  819. return _deviceClient.deleteAlias(alias, platform);
  820. }
  821. public Map<String, OnlineStatus> getUserOnlineStatus(String... registrationIds)
  822. throws APIConnectionException, APIRequestException
  823. {
  824. return _deviceClient.getUserOnlineStatus(registrationIds);
  825. }
  826. public DefaultResult bindMobile(String registrationId, String mobile)
  827. throws APIConnectionException, APIRequestException
  828. {
  829. return _deviceClient.bindMobile(registrationId, mobile);
  830. }
  831. // ----------------------- Schedule
  832. /**
  833. * Create a single schedule.
  834. * @param name The schedule name.
  835. * @param time The push time, format is 'yyyy-MM-dd HH:mm:ss'
  836. * @param push The push payload.
  837. * @return The created scheduleResult instance.
  838. * @throws APIConnectionException if a remote or network exception occurs.
  839. * @throws APIRequestException if a request exception occurs.
  840. */
  841. public ScheduleResult createSingleSchedule(String name, String time, PushPayload push)
  842. throws APIConnectionException, APIRequestException {
  843. TriggerPayload trigger = TriggerPayload.newBuilder()
  844. .setSingleTime(time)
  845. .buildSingle();
  846. SchedulePayload payload = SchedulePayload.newBuilder()
  847. .setName(name)
  848. .setEnabled(true)
  849. .setTrigger(trigger)
  850. .setPush(push)
  851. .build();
  852. return _scheduleClient.createSchedule(payload);
  853. }
  854. /**
  855. * Create a daily schedule push everyday.
  856. * @param name The schedule name.
  857. * @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'.
  858. * @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'.
  859. * @param time The push time, format 'HH:mm:ss'
  860. * @param push The push payload.
  861. * @return The created scheduleResult instance.
  862. * @throws APIConnectionException if a remote or network exception occurs.
  863. * @throws APIRequestException if a request exception occurs.
  864. */
  865. public ScheduleResult createDailySchedule(String name, String start, String end, String time, PushPayload push)
  866. throws APIConnectionException, APIRequestException {
  867. return createPeriodicalSchedule(name, start, end, time, TimeUnit.DAY, 1, null, push);
  868. }
  869. /**
  870. * Create a daily schedule push with a custom frequency.
  871. * @param name The schedule name.
  872. * @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'.
  873. * @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'.
  874. * @param time The push time, format 'HH:mm:ss'
  875. * @param frequency The custom frequency.
  876. * @param push The push payload.
  877. * @return The created scheduleResult instance.
  878. * @throws APIConnectionException if a remote or network exception occurs.
  879. * @throws APIRequestException if a request exception occurs.
  880. */
  881. public ScheduleResult createDailySchedule(String name, String start, String end, String time, int frequency, PushPayload push)
  882. throws APIConnectionException, APIRequestException {
  883. return createPeriodicalSchedule(name, start, end, time, TimeUnit.DAY, frequency, null, push);
  884. }
  885. /**
  886. * Create a weekly schedule push every week at the appointed days.
  887. * @param name The schedule name.
  888. * @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'.
  889. * @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'.
  890. * @param time The push time, format 'HH:mm:ss'
  891. * @param days The appointed days.
  892. * @param push The push payload.
  893. * @return The created scheduleResult instance.
  894. * @throws APIConnectionException if a remote or network exception occurs.
  895. * @throws APIRequestException if a request exception occurs.
  896. */
  897. public ScheduleResult createWeeklySchedule(String name, String start, String end, String time, Week[] days, PushPayload push)
  898. throws APIConnectionException, APIRequestException {
  899. Preconditions.checkArgument(null != days && days.length > 0, "The days must not be empty.");
  900. String[] points = new String[days.length];
  901. for(int i = 0 ; i < days.length; i++) {
  902. points[i] = days[i].name();
  903. }
  904. return createPeriodicalSchedule(name, start, end, time, TimeUnit.WEEK, 1, points, push);
  905. }
  906. /**
  907. * Create a weekly schedule push with a custom frequency at the appointed days.
  908. * @param name The schedule name.
  909. * @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'.
  910. * @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'.
  911. * @param time The push time, format 'HH:mm:ss'.
  912. * @param frequency The custom frequency.
  913. * @param days The appointed days.
  914. * @param push The push payload.
  915. * @return The created scheduleResult instance.
  916. * @throws APIConnectionException if a remote or network exception occurs.
  917. * @throws APIRequestException if a request exception occurs.
  918. */
  919. public ScheduleResult createWeeklySchedule(String name, String start, String end, String time, int frequency, Week[] days, PushPayload push)
  920. throws APIConnectionException, APIRequestException {
  921. Preconditions.checkArgument(null != days && days.length > 0, "The days must not be empty.");
  922. String[] points = new String[days.length];
  923. for(int i = 0 ; i < days.length; i++) {
  924. points[i] = days[i].name();
  925. }
  926. return createPeriodicalSchedule(name, start, end, time, TimeUnit.WEEK, frequency, points, push);
  927. }
  928. /**
  929. * Create a monthly schedule push every month at the appointed days.
  930. * @param name The schedule name.
  931. * @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'.
  932. * @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'.
  933. * @param time The push time, format 'HH:mm:ss'.
  934. * @param points The appointed days.
  935. * @param push The push payload.
  936. * @return The created scheduleResult instance.
  937. * @throws APIConnectionException if a remote or network exception occurs.
  938. * @throws APIRequestException if a request exception occurs.
  939. */
  940. public ScheduleResult createMonthlySchedule(String name, String start, String end, String time, String[] points, PushPayload push)
  941. throws APIConnectionException, APIRequestException {
  942. Preconditions.checkArgument(null != points && points.length > 0, "The points must not be empty.");
  943. return createPeriodicalSchedule(name, start, end, time, TimeUnit.MONTH, 1, points, push);
  944. }
  945. /**
  946. * Create a monthly schedule push with a custom frequency at the appointed days.
  947. * @param name The schedule name.
  948. * @param start The schedule comes into effect date, format 'yyyy-MM-dd HH:mm:ss'.
  949. * @param end The schedule expiration date, format 'yyyy-MM-dd HH:mm:ss'.
  950. * @param time The push time, format 'HH:mm:ss'.
  951. * @param frequency The custom frequency.
  952. * @param points The appointed days.
  953. * @param push The push payload.
  954. * @return The created scheduleResult instance.
  955. * @throws APIConnectionException if a remote or network exception occurs.
  956. * @throws APIRequestException if a request exception occurs.
  957. */
  958. public ScheduleResult createMonthlySchedule(String name, String start, String end, String time, int frequency, String[] points, PushPayload push)
  959. throws APIConnectionException, APIRequestException {
  960. Preconditions.checkArgument(null != points && points.length > 0, "The points must not be empty.");
  961. return createPeriodicalSchedule(name, start, end, time, TimeUnit.MONTH, frequency, points, push);
  962. }
  963. /**
  964. * Get the schedule information by the schedule id.
  965. * @param scheduleId The schedule id.
  966. * @return The schedule information.
  967. * @throws APIConnectionException if a remote or network exception occurs.
  968. * @throws APIRequestException if a request exception occurs.
  969. */
  970. public ScheduleResult getSchedule(String scheduleId)
  971. throws APIConnectionException, APIRequestException {
  972. return _scheduleClient.getSchedule(scheduleId);
  973. }
  974. /**
  975. * Get the schedule list size and the first page.
  976. * @return The schedule list size and the first page.
  977. * @throws APIConnectionException if a remote or network exception occurs.
  978. * @throws APIRequestException if a request exception occurs.
  979. */
  980. public ScheduleListResult getScheduleList()
  981. throws APIConnectionException, APIRequestException {
  982. return _scheduleClient.getScheduleList(1);
  983. }
  984. /**
  985. * Get the schedule list by the page.
  986. * @param page The page to search.
  987. * @return The schedule list of the appointed page.
  988. * @throws APIConnectionException if a remote or network exception occurs.
  989. * @throws APIRequestException if a request exception occurs.
  990. */
  991. public ScheduleListResult getScheduleList(int page)
  992. throws APIConnectionException, APIRequestException {
  993. return _scheduleClient.getScheduleList(page);
  994. }
  995. /**
  996. * Update the schedule name
  997. * @param scheduleId The schedule id.
  998. * @param name The new name.
  999. * @return The schedule information after updated.
  1000. * @throws APIConnectionException if a remote or network exception occurs.
  1001. * @throws APIRequestException if a request exception occurs.
  1002. */
  1003. public ScheduleResult updateScheduleName(String scheduleId, String name)
  1004. throws APIConnectionException, APIRequestException {
  1005. SchedulePayload payload = SchedulePayload.newBuilder()
  1006. .setName(name)
  1007. .build();
  1008. return updateSchedule(scheduleId, payload);
  1009. }
  1010. /**
  1011. * Enable the schedule.
  1012. * @param scheduleId The schedule id.
  1013. * @return The schedule information after updated.
  1014. * @throws APIConnectionException if a remote or network exception occurs.
  1015. * @throws APIRequestException if a request exception occurs.
  1016. */
  1017. public ScheduleResult enableSchedule(String scheduleId)
  1018. throws APIConnectionException, APIRequestException {
  1019. SchedulePayload payload = SchedulePayload.newBuilder()
  1020. .setEnabled(true)
  1021. .build();
  1022. return updateSchedule(scheduleId, payload);
  1023. }
  1024. /**
  1025. * Disable the schedule.
  1026. * @param scheduleId The schedule id.
  1027. * @return The schedule information after updated.
  1028. * @throws APIConnectionException if a remote or network exception occurs.
  1029. * @throws APIRequestException if a request exception occurs.
  1030. */
  1031. public ScheduleResult disableSchedule(String scheduleId)
  1032. throws APIConnectionException, APIRequestException {
  1033. SchedulePayload payload = SchedulePayload.newBuilder()
  1034. .setEnabled(false)
  1035. .build();
  1036. return updateSchedule(scheduleId, payload);
  1037. }
  1038. /**
  1039. * Update the trigger of the schedule.
  1040. * @param scheduleId The schedule id.
  1041. * @param trigger The new trigger.
  1042. * @return The schedule information after updated.
  1043. * @throws APIConnectionException if a remote or network exception occurs.
  1044. * @throws APIRequestException if a request exception occurs.
  1045. */
  1046. public ScheduleResult updateScheduleTrigger(String scheduleId, TriggerPayload trigger)
  1047. throws APIConnectionException, APIRequestException {
  1048. SchedulePayload payload = SchedulePayload.newBuilder()
  1049. .setTrigger(trigger)
  1050. .build();
  1051. return updateSchedule(scheduleId, payload);
  1052. }
  1053. /**
  1054. * Update the push content of the schedule.
  1055. * @param scheduleId The schedule id.
  1056. * @param push The new push payload.
  1057. * @return The schedule information after updated.
  1058. * @throws APIConnectionException if a remote or network exception occurs.
  1059. * @throws APIRequestException if a request exception occurs.
  1060. */
  1061. public ScheduleResult updateSchedulePush(String scheduleId, PushPayload push)
  1062. throws APIConnectionException, APIRequestException {
  1063. SchedulePayload payload = SchedulePayload.newBuilder()
  1064. .setPush(push)
  1065. .build();
  1066. return updateSchedule(scheduleId, payload);
  1067. }
  1068. /**
  1069. * Update a schedule by the id.
  1070. * @param scheduleId The schedule id to update.
  1071. * @param payload The new schedule payload.
  1072. * @return The new schedule information.
  1073. * @throws APIConnectionException if a remote or network exception occurs.
  1074. * @throws APIRequestException if a request exception occurs.
  1075. */
  1076. public ScheduleResult updateSchedule(String scheduleId, SchedulePayload payload)
  1077. throws APIConnectionException, APIRequestException {
  1078. return _scheduleClient.updateSchedule(scheduleId, payload);
  1079. }
  1080. /**
  1081. * Delete a schedule by id.
  1082. * @param scheduleId The schedule id.
  1083. * @throws APIConnectionException if a remote or network exception occurs.
  1084. * @throws APIRequestException if a request exception occurs.
  1085. */
  1086. public void deleteSchedule(String scheduleId)
  1087. throws APIConnectionException, APIRequestException {
  1088. _scheduleClient.deleteSchedule(scheduleId);
  1089. }
  1090. private ScheduleResult createPeriodicalSchedule(String name, String start, String end, String time,
  1091. TimeUnit timeUnit, int frequency, String[] point, PushPayload push)
  1092. throws APIConnectionException, APIRequestException {
  1093. TriggerPayload trigger = TriggerPayload.newBuilder()
  1094. .setPeriodTime(start, end, time)
  1095. .setTimeFrequency(timeUnit, frequency, point )
  1096. .buildPeriodical();
  1097. SchedulePayload payload = SchedulePayload.newBuilder()
  1098. .setName(name)
  1099. .setEnabled(true)
  1100. .setTrigger(trigger)
  1101. .setPush(push)
  1102. .build();
  1103. return _scheduleClient.createSchedule(payload);
  1104. }
  1105. public void close() {
  1106. _pushClient.close();
  1107. }
  1108. }