From 9b9603f8cd75f77130658ce3ceeeed35bca50302 Mon Sep 17 00:00:00 2001 From: KenChoi Date: Wed, 6 Sep 2017 14:43:52 +0800 Subject: [PATCH 1/2] add getMessageStatus API --- README.md | 4 +- .../cn/jpush/api/examples/PushExample.java | 18 +-- .../cn/jpush/api/examples/ReportsExample.java | 29 +++++ pom.xml | 6 +- src/main/java/cn/jpush/api/JPushClient.java | 11 +- .../cn/jpush/api/report/MessageStatus.java | 28 +++++ .../cn/jpush/api/report/ReportClient.java | 13 ++ .../api/report/model/CheckMessagePayload.java | 115 ++++++++++++++++++ src/test/java/cn/jpush/api/BaseTest.java | 2 +- .../cn/jpush/api/push/PushClientTest.java | 2 +- .../jpush/api/report/ReportFunctionTests.java | 32 ++++- 11 files changed, 240 insertions(+), 20 deletions(-) create mode 100644 src/main/java/cn/jpush/api/report/MessageStatus.java create mode 100644 src/main/java/cn/jpush/api/report/model/CheckMessagePayload.java diff --git a/README.md b/README.md index 1cc1eaaa..d4faa953 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ cn.jpush.api jpush-client - 3.2.20 + 3.3.1 ``` ### jar 包方式 @@ -46,7 +46,7 @@ cn.jpush.api jiguang-common - 1.0.6 + 1.0.8 io.netty diff --git a/example/main/java/cn/jpush/api/examples/PushExample.java b/example/main/java/cn/jpush/api/examples/PushExample.java index 95419d23..35f7532d 100644 --- a/example/main/java/cn/jpush/api/examples/PushExample.java +++ b/example/main/java/cn/jpush/api/examples/PushExample.java @@ -37,7 +37,7 @@ public class PushExample { // demo App defined in resources/jpush-api.conf protected static final String APP_KEY ="d4ee2375846bc30fa51334f5"; - protected static final String MASTER_SECRET = "61807e56ddaebf2e47172159"; + protected static final String MASTER_SECRET = "f3b222f7e0dde430b6d8fa5a"; protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c"; protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e"; @@ -52,7 +52,8 @@ public class PushExample { public static void main(String[] args) { // testSendPushWithCustomConfig(); // testSendIosAlert(); - testSendPush(); +// testSendPush(); + testGetCidList(); // testSendPushes(); // testSendPush_fromJSON(); // testSendPushWithCallback(); @@ -81,14 +82,12 @@ public void onSucceed(ResponseWrapper responseWrapper) { public static void testSendPush() { ClientConfig clientConfig = ClientConfig.getInstance(); final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig); - String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET); // Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient. - NativeHttpClient httpClient = new NativeHttpClient(authCode, null, clientConfig); // Call setHttpClient to set httpClient, // If you don't invoke this method, default httpClient will use NativeHttpClient. // ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig); // jpushClient.getPushClient().setHttpClient(httpClient); - final PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(); + final PushPayload payload = buildPushObject_android_and_ios(); // // For push, all you need do is to build PushPayload object. // PushPayload payload = buildPushObject_all_alias_alert(); try { @@ -226,13 +225,16 @@ public static PushPayload buildPushObject_android_tag_alertWithTitle() { } public static PushPayload buildPushObject_android_and_ios() { + Map extras = new HashMap(); + extras.put("test", "https://community.jiguang.cn/push"); return PushPayload.newBuilder() .setPlatform(Platform.android_ios()) - .setAudience(Audience.tag("tag1")) + .setAudience(Audience.all()) .setNotification(Notification.newBuilder() .setAlert("alert content") .addPlatformNotification(AndroidNotification.newBuilder() - .setTitle("Android Title").build()) + .setTitle("Android Title") + .addExtras(extras).build()) .addPlatformNotification(IosNotification.newBuilder() .incrBadge(1) .addExtra("extra_key", "extra_value").build()) @@ -416,7 +418,7 @@ public static void testSendWithSMS() { public static void testGetCidList() { JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY); try { - CIDResult result = jPushClient.getCidList(3, null); + CIDResult result = jPushClient.getCidList(3, "push"); LOG.info("Got result - " + result); } catch (APIConnectionException e) { LOG.error("Connection error. Should retry later. ", e); diff --git a/example/main/java/cn/jpush/api/examples/ReportsExample.java b/example/main/java/cn/jpush/api/examples/ReportsExample.java index 9b1723fd..019bdbbe 100644 --- a/example/main/java/cn/jpush/api/examples/ReportsExample.java +++ b/example/main/java/cn/jpush/api/examples/ReportsExample.java @@ -1,5 +1,7 @@ package cn.jpush.api.examples; +import cn.jpush.api.report.MessageStatus; +import cn.jpush.api.report.model.CheckMessagePayload; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,12 +13,17 @@ import cn.jpush.api.report.ReceivedsResult; import cn.jpush.api.report.UsersResult; +import java.util.Map; + public class ReportsExample { protected static final Logger LOG = LoggerFactory.getLogger(ReportsExample.class); // demo App defined in resources/jpush-api.conf private static final String appKey = "dd1066407b044738b6479275"; private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7"; + public static final String REGISTRATION_ID1 = "0900e8d85ef"; + public static final String REGISTRATION_ID2 = "0a04ad7d8b4"; + public static final String REGISTRATION_ID3 = "18071adc030dcba91c0"; public static void main(String[] args) { testGetReport(); @@ -76,5 +83,27 @@ public static void testGetMessages() { } } + public static void testGetMessageStatus() { + JPushClient jPushClient = new JPushClient(masterSecret, appKey); + CheckMessagePayload payload = CheckMessagePayload.newBuilder() + .setMsgId(3993287034L) + .addRegistrationIds(REGISTRATION_ID1, REGISTRATION_ID2, REGISTRATION_ID3) + .setDate("2017-08-08") + .build(); + try { + Map map = jPushClient.getMessageStatus(payload); + for (Map.Entry entry : map.entrySet()) { + LOG.info("registrationId: " + entry.getKey() + " status: " + entry.getValue().getStatus()); + } + } 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()); + } + } + } diff --git a/pom.xml b/pom.xml index 818cce8a..fbb5d093 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ cn.jpush.api jpush-client - 3.3.0-SNAPSHOT + 3.3.1-SNAPSHOT jar https://github.com/jpush/jpush-api-java-client JPush API Java Client @@ -35,14 +35,14 @@ https://github.com/jpush/jpush-api-java-client scm:git:git@github.com:jpush/jpush-api-java-client.git scm:git:git@github.com:jpush/jpush-api-java-client.git - v3.3.0 + v3.3.1 cn.jpush.api jiguang-common - 1.0.7 + 1.0.8 org.apache.httpcomponents diff --git a/src/main/java/cn/jpush/api/JPushClient.java b/src/main/java/cn/jpush/api/JPushClient.java index 418579db..c43a0abb 100644 --- a/src/main/java/cn/jpush/api/JPushClient.java +++ b/src/main/java/cn/jpush/api/JPushClient.java @@ -4,6 +4,8 @@ import java.util.Set; import cn.jpush.api.push.CIDResult; +import cn.jpush.api.report.*; +import cn.jpush.api.report.model.CheckMessagePayload; import com.google.gson.JsonObject; import cn.jiguang.common.ClientConfig; @@ -29,10 +31,6 @@ import cn.jpush.api.push.model.audience.Audience; import cn.jpush.api.push.model.notification.IosAlert; import cn.jpush.api.push.model.notification.Notification; -import cn.jpush.api.report.MessagesResult; -import cn.jpush.api.report.ReceivedsResult; -import cn.jpush.api.report.ReportClient; -import cn.jpush.api.report.UsersResult; import cn.jpush.api.schedule.ScheduleClient; import cn.jpush.api.schedule.ScheduleListResult; import cn.jpush.api.schedule.ScheduleResult; @@ -261,6 +259,11 @@ public UsersResult getReportUsers(TimeUnit timeUnit, String start, int duration) public MessagesResult getReportMessages(String msgIds) throws APIConnectionException, APIRequestException { return _reportClient.getMessages(msgIds); } + + public Map getMessageStatus(CheckMessagePayload payload) + throws APIConnectionException, APIRequestException { + return _reportClient.getMessagesStatus(payload); + } // ------------------------------ Shortcuts - notification diff --git a/src/main/java/cn/jpush/api/report/MessageStatus.java b/src/main/java/cn/jpush/api/report/MessageStatus.java new file mode 100644 index 00000000..de3f902d --- /dev/null +++ b/src/main/java/cn/jpush/api/report/MessageStatus.java @@ -0,0 +1,28 @@ +package cn.jpush.api.report; + +import java.io.Serializable; + +public class MessageStatus implements Serializable { + + /** + * 0 送达 + * 1 未送达 + * 2 用户不属于该 app + * 3 用户属于该 app,但不属于该 msgid + * 4 系统异常 + */ + private int status; + + public void setStatus(int status) { + this.status = status; + } + + public int getStatus() { + return status; + } + + @Override + public String toString() { + return "status : " + status; + } +} diff --git a/src/main/java/cn/jpush/api/report/ReportClient.java b/src/main/java/cn/jpush/api/report/ReportClient.java index 12b113ce..c5047a41 100644 --- a/src/main/java/cn/jpush/api/report/ReportClient.java +++ b/src/main/java/cn/jpush/api/report/ReportClient.java @@ -1,7 +1,9 @@ package cn.jpush.api.report; +import java.lang.reflect.Type; import java.net.URLEncoder; +import java.util.Map; import java.util.regex.Pattern; import cn.jiguang.common.ClientConfig; @@ -14,6 +16,9 @@ import cn.jiguang.common.resp.APIRequestException; import cn.jiguang.common.resp.BaseResult; import cn.jiguang.common.resp.ResponseWrapper; +import cn.jpush.api.report.model.CheckMessagePayload; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; public class ReportClient { @@ -100,6 +105,14 @@ public MessagesResult getMessages(String msgIds) return MessagesResult.fromResponse(response); } + + public Map getMessagesStatus(CheckMessagePayload payload) + throws APIConnectionException, APIRequestException { + String url = _hostName + "/v3/status/message"; + ResponseWrapper result = _httpClient.sendPost(url, payload.toString()); + Type type = new TypeToken>(){}.getType(); + return new Gson().fromJson(result.responseContent, type); + } public UsersResult getUsers(TimeUnit timeUnit, String start, int duration) throws APIConnectionException, APIRequestException { diff --git a/src/main/java/cn/jpush/api/report/model/CheckMessagePayload.java b/src/main/java/cn/jpush/api/report/model/CheckMessagePayload.java new file mode 100644 index 00000000..95ba6dd6 --- /dev/null +++ b/src/main/java/cn/jpush/api/report/model/CheckMessagePayload.java @@ -0,0 +1,115 @@ +package cn.jpush.api.report.model; + +import cn.jiguang.common.utils.Preconditions; +import cn.jiguang.common.utils.TimeUtils; +import cn.jpush.api.schedule.model.IModel; +import com.google.gson.*; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class CheckMessagePayload implements IModel{ + + public final static String MSG_ID = "msg_id"; + public final static String REGISTRATION_IDS = "registration_ids"; + public final static String DATE = "date"; + + private long msgId = -1L; + private List registrationIds; + private String date; + private Gson gson = new Gson(); + public CheckMessagePayload(long msgId, List rids, String date) { + this.msgId = msgId; + this.registrationIds = rids; + this.date = date; + } + + public static Builder newBuilder() { + return new Builder(); + } + + public static class Builder { + private long msgId; + private List registrationIds = new ArrayList(); + private String date; + + public Builder setMsgId(long msgId) { + this.msgId = msgId; + return this; + } + + public Builder setRegistrationIds(String[] rids) { + Preconditions.checkArgument(rids != null && rids.length > 0, "Registration ids is empty"); + Collections.addAll(registrationIds, rids); + return this; + } + + public Builder setRegistrationsIds(List rids) { + Preconditions.checkArgument(rids != null && rids.size() > 0, "Registration ids is empty"); + registrationIds = rids; + return this; + } + + public Builder addRegistrationIds(String... rids) { + if (null == rids) { + return this; + } + Collections.addAll(registrationIds, rids); + return this; + } + + public Builder addRegistrationIds(List rids) { + registrationIds.addAll(rids); + return this; + } + + public Builder setDate(String date) { + Preconditions.checkArgument(isDayFormat(date), "Date format is invalid"); + this.date = date; + return this; + } + + public CheckMessagePayload build() { + return new CheckMessagePayload(msgId, registrationIds, date); + } + } + + @Override + public JsonElement toJSON() { + JsonObject jsonObject = new JsonObject(); + if (msgId != -1L) { + jsonObject.addProperty(MSG_ID, msgId); + } + if (null != registrationIds) { + JsonArray jsonArray = new JsonArray(); + for (String rid : registrationIds) { + jsonArray.add(new JsonPrimitive(rid)); + } + jsonObject.add(REGISTRATION_IDS, jsonArray); + } + if (null != date) { + jsonObject.addProperty(DATE, date); + } + return jsonObject; + } + + @Override + public String toString() { + return gson.toJson(toJSON()); + } + + private static boolean isDayFormat(String date) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + try { + simpleDateFormat.parse(date); + return true; + } catch (ParseException e) { + e.printStackTrace(); + return false; + } + } +} diff --git a/src/test/java/cn/jpush/api/BaseTest.java b/src/test/java/cn/jpush/api/BaseTest.java index 872036b4..83c5719a 100644 --- a/src/test/java/cn/jpush/api/BaseTest.java +++ b/src/test/java/cn/jpush/api/BaseTest.java @@ -5,7 +5,7 @@ public abstract class BaseTest { protected static final String APP_KEY = "d4ee2375846bc30fa51334f5"; - protected static final String MASTER_SECRET = "61807e56ddaebf2e47172159"; + protected static final String MASTER_SECRET = "f88f9b289c3f681eef0b95ee"; protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e"; protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c"; diff --git a/src/test/java/cn/jpush/api/push/PushClientTest.java b/src/test/java/cn/jpush/api/push/PushClientTest.java index 05ec0576..76177893 100644 --- a/src/test/java/cn/jpush/api/push/PushClientTest.java +++ b/src/test/java/cn/jpush/api/push/PushClientTest.java @@ -131,7 +131,7 @@ public void test_validate() { public void testGetCidList() { JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY); try { - CIDResult result = jPushClient.getCidList(3, null); + CIDResult result = jPushClient.getCidList(3, "push"); LOG.info("Got result - " + result); } catch (APIConnectionException e) { LOG.error("Connection error. Should retry later. ", e); diff --git a/src/test/java/cn/jpush/api/report/ReportFunctionTests.java b/src/test/java/cn/jpush/api/report/ReportFunctionTests.java index 6f2026c1..a1cb428c 100644 --- a/src/test/java/cn/jpush/api/report/ReportFunctionTests.java +++ b/src/test/java/cn/jpush/api/report/ReportFunctionTests.java @@ -2,16 +2,26 @@ import static org.junit.Assert.assertTrue; +import cn.jiguang.common.resp.APIConnectionException; +import cn.jiguang.common.resp.APIRequestException; +import cn.jpush.api.push.model.Message; +import cn.jpush.api.report.model.CheckMessagePayload; import org.junit.Test; import org.junit.experimental.categories.Category; import cn.jiguang.common.TimeUnit; import cn.jpush.api.BaseTest; import cn.jpush.api.SlowTests; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; @Category(SlowTests.class) public class ReportFunctionTests extends BaseTest { + private static final Logger LOG = LoggerFactory.getLogger(ReportFunctionTests.class); + @Test public void getReceivedsFixed() throws Exception { ReceivedsResult result = jpushClient.getReportReceiveds("1613113584,1229760629,1174658841,1174658641"); @@ -56,7 +66,27 @@ public void getUserTest3() throws Exception { assertTrue(result.items.size() > 0); } - + @Test + public void testGetMessageStatus() { + CheckMessagePayload payload = CheckMessagePayload.newBuilder() + .setMsgId(3993287034L) + .addRegistrationIds(REGISTRATION_ID1, REGISTRATION_ID2, REGISTRATION_ID3) + .setDate("2017-08-08") + .build(); + try { + Map map = jpushClient.getMessageStatus(payload); + for (Map.Entry entry : map.entrySet()) { + LOG.info("registrationId: " + entry.getKey() + " status: " + entry.getValue().getStatus()); + } + } 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()); + } + } } From be8debcc7e2971438d692e46f3ce69c83e8dc247 Mon Sep 17 00:00:00 2001 From: KenChoi Date: Wed, 6 Sep 2017 14:58:50 +0800 Subject: [PATCH 2/2] update test case, appkey not exist error code is 1004 --- src/test/java/cn/jpush/api/push/remote/ExceptionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/cn/jpush/api/push/remote/ExceptionTest.java b/src/test/java/cn/jpush/api/push/remote/ExceptionTest.java index b80730b1..1d307fa0 100644 --- a/src/test/java/cn/jpush/api/push/remote/ExceptionTest.java +++ b/src/test/java/cn/jpush/api/push/remote/ExceptionTest.java @@ -30,7 +30,7 @@ public void appKeyNotExist() { } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIRequestException e) { - assertEquals(APPKEY_NOT_EXIST, e.getErrorCode()); + assertEquals(AUTHENTICATION_FAIL, e.getErrorCode()); } }