|
1 | 1 | package cn.jpush.api; |
2 | 2 |
|
| 3 | +import java.util.Map; |
| 4 | + |
3 | 5 | import cn.jpush.api.common.APIConnectionException; |
4 | 6 | import cn.jpush.api.common.APIRequestException; |
5 | 7 | import cn.jpush.api.common.TimeUnit; |
6 | 8 | import cn.jpush.api.push.PushClient; |
7 | 9 | import cn.jpush.api.push.PushResult; |
| 10 | +import cn.jpush.api.push.model.Message; |
| 11 | +import cn.jpush.api.push.model.Platform; |
8 | 12 | import cn.jpush.api.push.model.PushPayload; |
| 13 | +import cn.jpush.api.push.model.audience.Audience; |
| 14 | +import cn.jpush.api.push.model.notification.AndroidNotification; |
| 15 | +import cn.jpush.api.push.model.notification.IosNotification; |
| 16 | +import cn.jpush.api.push.model.notification.Notification; |
9 | 17 | import cn.jpush.api.report.MessagesResult; |
10 | 18 | import cn.jpush.api.report.ReceivedsResult; |
11 | 19 | import cn.jpush.api.report.ReportClient; |
@@ -50,7 +58,7 @@ public JPushClient(String masterSecret, String appKey, boolean apnsProduction, l |
50 | 58 | } |
51 | 59 |
|
52 | 60 | /** |
53 | | - * Send a push with object. |
| 61 | + * Send a push with PushPayload object. |
54 | 62 | * |
55 | 63 | * @param pushPayload payload object of a push. |
56 | 64 | * @return PushResult The result object of a Push. Can be printed to a JSON. |
@@ -97,6 +105,173 @@ public MessagesResult getReportMessages(String msgIds) throws APIConnectionExcep |
97 | 105 | return _reportClient.getMessages(msgIds); |
98 | 106 | } |
99 | 107 |
|
| 108 | + |
| 109 | + // ------------------------------ Shortcuts - notification |
| 110 | + |
| 111 | + /** |
| 112 | + * Shortcut |
| 113 | + */ |
| 114 | + public PushResult sendNotificationAll(String alert) throws APIConnectionException, APIRequestException { |
| 115 | + PushPayload payload = PushPayload.alertAll(alert); |
| 116 | + return _pushClient.sendPush(payload); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Shortcut |
| 121 | + */ |
| 122 | + public PushResult sendAndroidNotificationWithAlias(String title, String alert, |
| 123 | + Map<String, String> extras, String... alias) |
| 124 | + throws APIConnectionException, APIRequestException { |
| 125 | + PushPayload payload = PushPayload.newBuilder() |
| 126 | + .setPlatform(Platform.android()) |
| 127 | + .setAudience(Audience.alias(alias)) |
| 128 | + .setNotification(Notification.newBuilder() |
| 129 | + .addPlatformNotification(AndroidNotification.newBuilder() |
| 130 | + .setTitle(title) |
| 131 | + .setAlert(alert) |
| 132 | + .addExtras(extras) |
| 133 | + .build()) |
| 134 | + .build()) |
| 135 | + .build(); |
| 136 | + return _pushClient.sendPush(payload); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Shortcut |
| 141 | + */ |
| 142 | + public PushResult sendAndroidNotificationWithRegistrationID(String title, String alert, |
| 143 | + Map<String, String> extras, String... registrationID) |
| 144 | + throws APIConnectionException, APIRequestException { |
| 145 | + PushPayload payload = PushPayload.newBuilder() |
| 146 | + .setPlatform(Platform.android()) |
| 147 | + .setAudience(Audience.registrationId(registrationID)) |
| 148 | + .setNotification(Notification.newBuilder() |
| 149 | + .addPlatformNotification(AndroidNotification.newBuilder() |
| 150 | + .setTitle(title) |
| 151 | + .setAlert(alert) |
| 152 | + .addExtras(extras) |
| 153 | + .build()) |
| 154 | + .build()) |
| 155 | + .build(); |
| 156 | + return _pushClient.sendPush(payload); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Shortcut |
| 161 | + */ |
| 162 | + public PushResult sendIosNotificationWithAlias(String alert, |
| 163 | + Map<String, String> extras, String... alias) |
| 164 | + throws APIConnectionException, APIRequestException { |
| 165 | + PushPayload payload = PushPayload.newBuilder() |
| 166 | + .setPlatform(Platform.ios()) |
| 167 | + .setAudience(Audience.alias(alias)) |
| 168 | + .setNotification(Notification.newBuilder() |
| 169 | + .addPlatformNotification(IosNotification.newBuilder() |
| 170 | + .setAlert(alert) |
| 171 | + .addExtras(extras) |
| 172 | + .build()) |
| 173 | + .build()) |
| 174 | + .build(); |
| 175 | + return _pushClient.sendPush(payload); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Shortcut |
| 180 | + */ |
| 181 | + public PushResult sendIosNotificationWithRegistrationID(String alert, |
| 182 | + Map<String, String> extras, String... registrationID) |
| 183 | + throws APIConnectionException, APIRequestException { |
| 184 | + PushPayload payload = PushPayload.newBuilder() |
| 185 | + .setPlatform(Platform.ios()) |
| 186 | + .setAudience(Audience.registrationId(registrationID)) |
| 187 | + .setNotification(Notification.newBuilder() |
| 188 | + .addPlatformNotification(IosNotification.newBuilder() |
| 189 | + .setAlert(alert) |
| 190 | + .addExtras(extras) |
| 191 | + .build()) |
| 192 | + .build()) |
| 193 | + .build(); |
| 194 | + return _pushClient.sendPush(payload); |
| 195 | + } |
| 196 | + |
| 197 | + |
| 198 | + // ---------------------- shortcuts - message |
| 199 | + |
| 200 | + /** |
| 201 | + * Shortcut |
| 202 | + */ |
| 203 | + public PushResult sendMessageAll(String msgContent) throws APIConnectionException, APIRequestException { |
| 204 | + PushPayload payload = PushPayload.messageAll(msgContent); |
| 205 | + return _pushClient.sendPush(payload); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Shortcut |
| 210 | + */ |
| 211 | + public PushResult sendAndroidMessageWithAlias(String title, String msgContent, String... alias) |
| 212 | + throws APIConnectionException, APIRequestException { |
| 213 | + PushPayload payload = PushPayload.newBuilder() |
| 214 | + .setPlatform(Platform.android()) |
| 215 | + .setAudience(Audience.alias(alias)) |
| 216 | + .setMessage(Message.newBuilder() |
| 217 | + .setTitle(title) |
| 218 | + .setMsgContent(msgContent) |
| 219 | + .build()) |
| 220 | + .build(); |
| 221 | + return _pushClient.sendPush(payload); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Shortcut |
| 226 | + */ |
| 227 | + public PushResult sendAndroidMessageWithRegistrationID(String title, String msgContent, String... registrationID) |
| 228 | + throws APIConnectionException, APIRequestException { |
| 229 | + PushPayload payload = PushPayload.newBuilder() |
| 230 | + .setPlatform(Platform.android()) |
| 231 | + .setAudience(Audience.registrationId(registrationID)) |
| 232 | + .setMessage(Message.newBuilder() |
| 233 | + .setTitle(title) |
| 234 | + .setMsgContent(msgContent) |
| 235 | + .build()) |
| 236 | + .build(); |
| 237 | + return _pushClient.sendPush(payload); |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * Shortcut |
| 242 | + */ |
| 243 | + public PushResult sendIosMessageWithAlias(String title, String msgContent, String... alias) |
| 244 | + throws APIConnectionException, APIRequestException { |
| 245 | + PushPayload payload = PushPayload.newBuilder() |
| 246 | + .setPlatform(Platform.ios()) |
| 247 | + .setAudience(Audience.alias(alias)) |
| 248 | + .setMessage(Message.newBuilder() |
| 249 | + .setTitle(title) |
| 250 | + .setMsgContent(msgContent) |
| 251 | + .build()) |
| 252 | + .build(); |
| 253 | + return _pushClient.sendPush(payload); |
| 254 | + } |
| 255 | + |
| 256 | + /** |
| 257 | + * Shortcut |
| 258 | + */ |
| 259 | + public PushResult sendIosMessageWithRegistrationID(String title, String msgContent, String... registrationID) |
| 260 | + throws APIConnectionException, APIRequestException { |
| 261 | + PushPayload payload = PushPayload.newBuilder() |
| 262 | + .setPlatform(Platform.ios()) |
| 263 | + .setAudience(Audience.registrationId(registrationID)) |
| 264 | + .setMessage(Message.newBuilder() |
| 265 | + .setTitle(title) |
| 266 | + .setMsgContent(msgContent) |
| 267 | + .build()) |
| 268 | + .build(); |
| 269 | + return _pushClient.sendPush(payload); |
| 270 | + } |
| 271 | + |
| 272 | + |
| 273 | + |
| 274 | + |
100 | 275 |
|
101 | 276 | } |
102 | 277 |
|
0 commit comments