|
24 | 24 | <dependency> |
25 | 25 | <groupId>cn.jpush.api</groupId> |
26 | 26 | <artifactId>jpush-client</artifactId> |
27 | | - <version>3.2.5</version> |
| 27 | + <version>3.2.6</version> |
28 | 28 | </dependency> |
29 | 29 | ``` |
30 | 30 | ### jar 包方式 |
|
87 | 87 | > 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.PushExample |
88 | 88 |
|
89 | 89 | ```Java |
90 | | - JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3); |
91 | | - |
92 | | - // For push, all you need do is to build PushPayload object. |
93 | | - PushPayload payload = buildPushObject_all_all_alert(); |
94 | | - |
95 | | - try { |
96 | | - PushResult result = jpushClient.sendPush(payload); |
97 | | - LOG.info("Got result - " + result); |
98 | | - |
99 | | - } catch (APIConnectionException e) { |
100 | | - // Connection error, should retry later |
101 | | - LOG.error("Connection error, should retry later", e); |
102 | | - |
103 | | - } catch (APIRequestException e) { |
104 | | - // Should review the error, and fix the request |
105 | | - LOG.error("Should review the error, and fix the request", e); |
106 | | - LOG.info("HTTP Status: " + e.getStatus()); |
107 | | - LOG.info("Error Code: " + e.getErrorCode()); |
108 | | - LOG.info("Error Message: " + e.getErrorMessage()); |
109 | | - } |
| 90 | + JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3); |
110 | 91 |
|
| 92 | + // For push, all you need do is to build PushPayload object. |
| 93 | + PushPayload payload = buildPushObject_all_all_alert(); |
| 94 | + |
| 95 | + try { |
| 96 | + PushResult result = jpushClient.sendPush(payload); |
| 97 | + LOG.info("Got result - " + result); |
| 98 | + |
| 99 | + } catch (APIConnectionException e) { |
| 100 | + // Connection error, should retry later |
| 101 | + LOG.error("Connection error, should retry later", e); |
| 102 | + |
| 103 | + } catch (APIRequestException e) { |
| 104 | + // Should review the error, and fix the request |
| 105 | + LOG.error("Should review the error, and fix the request", e); |
| 106 | + LOG.info("HTTP Status: " + e.getStatus()); |
| 107 | + LOG.info("Error Code: " + e.getErrorCode()); |
| 108 | + LOG.info("Error Message: " + e.getErrorMessage()); |
| 109 | + } |
111 | 110 | ``` |
112 | 111 |
|
113 | 112 | 进行推送的关键在于构建一个 PushPayload 对象。以下示例一般的构建对象的用法。 |
|
190 | 189 | > 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.ReportsExample |
191 | 190 |
|
192 | 191 | ```Java |
193 | | - JPushClient jpushClient = new JPushClient(masterSecret, appKey); |
194 | | - try { |
195 | | - ReceivedsResult result = jpushClient.getReportReceiveds("1942377665"); |
196 | | - LOG.debug("Got result - " + result); |
197 | | - |
198 | | - } catch (APIConnectionException e) { |
199 | | - // Connection error, should retry later |
200 | | - LOG.error("Connection error, should retry later", e); |
201 | | - |
202 | | - } catch (APIRequestException e) { |
203 | | - // Should review the error, and fix the request |
204 | | - LOG.error("Should review the error, and fix the request", e); |
205 | | - LOG.info("HTTP Status: " + e.getStatus()); |
206 | | - LOG.info("Error Code: " + e.getErrorCode()); |
207 | | - LOG.info("Error Message: " + e.getErrorMessage()); |
208 | | - } |
| 192 | + JPushClient jpushClient = new JPushClient(masterSecret, appKey); |
| 193 | + try { |
| 194 | + ReceivedsResult result = jpushClient.getReportReceiveds("1942377665"); |
| 195 | + LOG.debug("Got result - " + result); |
| 196 | + |
| 197 | + } catch (APIConnectionException e) { |
| 198 | + // Connection error, should retry later |
| 199 | + LOG.error("Connection error, should retry later", e); |
| 200 | + |
| 201 | + } catch (APIRequestException e) { |
| 202 | + // Should review the error, and fix the request |
| 203 | + LOG.error("Should review the error, and fix the request", e); |
| 204 | + LOG.info("HTTP Status: " + e.getStatus()); |
| 205 | + LOG.info("Error Code: " + e.getErrorCode()); |
| 206 | + LOG.info("Error Message: " + e.getErrorMessage()); |
| 207 | + } |
209 | 208 | ``` |
210 | 209 |
|
211 | 210 | ### Tag/Alias 样例 |
212 | 211 |
|
213 | 212 | > 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.DeviceExample |
214 | 213 |
|
215 | 214 | ```Java |
216 | | - try { |
217 | | - TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1); |
218 | | - |
219 | | - LOG.info(result.alias); |
220 | | - LOG.info(result.tags.toString()); |
221 | | - |
222 | | - } catch (APIConnectionException e) { |
223 | | - LOG.error("Connection error. Should retry later. ", e); |
224 | | - |
225 | | - } catch (APIRequestException e) { |
226 | | - LOG.error("Error response from JPush server. Should review and fix it. ", e); |
227 | | - LOG.info("HTTP Status: " + e.getStatus()); |
228 | | - LOG.info("Error Code: " + e.getErrorCode()); |
229 | | - LOG.info("Error Message: " + e.getErrorMessage()); |
230 | | - } |
| 215 | + try { |
| 216 | + TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1); |
| 217 | + |
| 218 | + LOG.info(result.alias); |
| 219 | + LOG.info(result.tags.toString()); |
| 220 | + } catch (APIConnectionException e) { |
| 221 | + LOG.error("Connection error. Should retry later. ", e); |
| 222 | + } catch (APIRequestException e) { |
| 223 | + LOG.error("Error response from JPush server. Should review and fix it. ", e); |
| 224 | + LOG.info("HTTP Status: " + e.getStatus()); |
| 225 | + LOG.info("Error Code: " + e.getErrorCode()); |
| 226 | + LOG.info("Error Message: " + e.getErrorMessage()); |
| 227 | + } |
| 228 | +``` |
| 229 | + |
| 230 | +### Schedule 样例 |
| 231 | + |
| 232 | +> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.ScheduleExample |
| 233 | +
|
| 234 | +```Java |
| 235 | + JPushClient jpushClient = new JPushClient(masterSecret, appKey); |
| 236 | + String name = "test_schedule_example"; |
| 237 | + String time = "2016-07-30 12:30:25"; |
| 238 | + PushPayload push = PushPayload.alertAll("test schedule example."); |
| 239 | + try { |
| 240 | + ScheduleResult result = jpushClient.createSingleSchedule(name, time, push); |
| 241 | + LOG.info("schedule result is " + result); |
| 242 | + } catch (APIConnectionException e) { |
| 243 | + LOG.error("Connection error. Should retry later. ", e); |
| 244 | + } catch (APIRequestException e) { |
| 245 | + LOG.error("Error response from JPush server. Should review and fix it. ", e); |
| 246 | + LOG.info("HTTP Status: " + e.getStatus()); |
| 247 | + LOG.info("Error Code: " + e.getErrorCode()); |
| 248 | + LOG.info("Error Message: " + e.getErrorMessage()); |
| 249 | + } |
231 | 250 | ``` |
0 commit comments