Skip to content

Commit 7fc2b03

Browse files
authored
Merge pull request #90 from jpush/dev
Dev
2 parents e5900ef + be8debc commit 7fc2b03

File tree

12 files changed

+240
-20
lines changed

12 files changed

+240
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>cn.jpush.api</groupId>
2828
<artifactId>jpush-client</artifactId>
29-
<version>3.3.0</version>
29+
<version>3.3.1</version>
3030
</dependency>
3131
```
3232
### jar 包方式
@@ -46,7 +46,7 @@
4646
<dependency>
4747
<groupId>cn.jpush.api</groupId>
4848
<artifactId>jiguang-common</artifactId>
49-
<version>1.0.7</version>
49+
<version>1.0.8</version>
5050
</dependency>
5151
<dependency>
5252
<groupId>io.netty</groupId>

example/main/java/cn/jpush/api/examples/PushExample.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class PushExample {
3737

3838
// demo App defined in resources/jpush-api.conf
3939
protected static final String APP_KEY ="d4ee2375846bc30fa51334f5";
40-
protected static final String MASTER_SECRET = "61807e56ddaebf2e47172159";
40+
protected static final String MASTER_SECRET = "f3b222f7e0dde430b6d8fa5a";
4141
protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c";
4242
protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e";
4343

@@ -52,7 +52,8 @@ public class PushExample {
5252
public static void main(String[] args) {
5353
// testSendPushWithCustomConfig();
5454
// testSendIosAlert();
55-
testSendPush();
55+
// testSendPush();
56+
testGetCidList();
5657
// testSendPushes();
5758
// testSendPush_fromJSON();
5859
// testSendPushWithCallback();
@@ -81,14 +82,12 @@ public void onSucceed(ResponseWrapper responseWrapper) {
8182
public static void testSendPush() {
8283
ClientConfig clientConfig = ClientConfig.getInstance();
8384
final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
84-
String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
8585
// Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient.
86-
NativeHttpClient httpClient = new NativeHttpClient(authCode, null, clientConfig);
8786
// Call setHttpClient to set httpClient,
8887
// If you don't invoke this method, default httpClient will use NativeHttpClient.
8988
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
9089
// jpushClient.getPushClient().setHttpClient(httpClient);
91-
final PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage();
90+
final PushPayload payload = buildPushObject_android_and_ios();
9291
// // For push, all you need do is to build PushPayload object.
9392
// PushPayload payload = buildPushObject_all_alias_alert();
9493
try {
@@ -226,13 +225,16 @@ public static PushPayload buildPushObject_android_tag_alertWithTitle() {
226225
}
227226

228227
public static PushPayload buildPushObject_android_and_ios() {
228+
Map<String, String> extras = new HashMap<String, String>();
229+
extras.put("test", "https://community.jiguang.cn/push");
229230
return PushPayload.newBuilder()
230231
.setPlatform(Platform.android_ios())
231-
.setAudience(Audience.tag("tag1"))
232+
.setAudience(Audience.all())
232233
.setNotification(Notification.newBuilder()
233234
.setAlert("alert content")
234235
.addPlatformNotification(AndroidNotification.newBuilder()
235-
.setTitle("Android Title").build())
236+
.setTitle("Android Title")
237+
.addExtras(extras).build())
236238
.addPlatformNotification(IosNotification.newBuilder()
237239
.incrBadge(1)
238240
.addExtra("extra_key", "extra_value").build())
@@ -416,7 +418,7 @@ public static void testSendWithSMS() {
416418
public static void testGetCidList() {
417419
JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
418420
try {
419-
CIDResult result = jPushClient.getCidList(3, null);
421+
CIDResult result = jPushClient.getCidList(3, "push");
420422
LOG.info("Got result - " + result);
421423
} catch (APIConnectionException e) {
422424
LOG.error("Connection error. Should retry later. ", e);

example/main/java/cn/jpush/api/examples/ReportsExample.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cn.jpush.api.examples;
22

3+
import cn.jpush.api.report.MessageStatus;
4+
import cn.jpush.api.report.model.CheckMessagePayload;
35
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
57

@@ -11,12 +13,17 @@
1113
import cn.jpush.api.report.ReceivedsResult;
1214
import cn.jpush.api.report.UsersResult;
1315

16+
import java.util.Map;
17+
1418
public class ReportsExample {
1519
protected static final Logger LOG = LoggerFactory.getLogger(ReportsExample.class);
1620

1721
// demo App defined in resources/jpush-api.conf
1822
private static final String appKey = "dd1066407b044738b6479275";
1923
private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7";
24+
public static final String REGISTRATION_ID1 = "0900e8d85ef";
25+
public static final String REGISTRATION_ID2 = "0a04ad7d8b4";
26+
public static final String REGISTRATION_ID3 = "18071adc030dcba91c0";
2027

2128
public static void main(String[] args) {
2229
testGetReport();
@@ -76,5 +83,27 @@ public static void testGetMessages() {
7683
}
7784
}
7885

86+
public static void testGetMessageStatus() {
87+
JPushClient jPushClient = new JPushClient(masterSecret, appKey);
88+
CheckMessagePayload payload = CheckMessagePayload.newBuilder()
89+
.setMsgId(3993287034L)
90+
.addRegistrationIds(REGISTRATION_ID1, REGISTRATION_ID2, REGISTRATION_ID3)
91+
.setDate("2017-08-08")
92+
.build();
93+
try {
94+
Map<String, MessageStatus> map = jPushClient.getMessageStatus(payload);
95+
for (Map.Entry<String, MessageStatus> entry : map.entrySet()) {
96+
LOG.info("registrationId: " + entry.getKey() + " status: " + entry.getValue().getStatus());
97+
}
98+
} catch (APIConnectionException e) {
99+
LOG.error("Connection error. Should retry later. ", e);
100+
} catch (APIRequestException e) {
101+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
102+
LOG.info("HTTP Status: " + e.getStatus());
103+
LOG.info("Error Code: " + e.getErrorCode());
104+
LOG.info("Error Message: " + e.getErrorMessage());
105+
}
106+
}
107+
79108
}
80109

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535
<url>https://github.com/jpush/jpush-api-java-client</url>
3636
<connection>scm:git:[email protected]:jpush/jpush-api-java-client.git</connection>
3737
<developerConnection>scm:git:[email protected]:jpush/jpush-api-java-client.git</developerConnection>
38-
<tag>v3.3.0</tag>
38+
<tag>v3.3.1</tag>
3939
</scm>
4040

4141
<dependencies>
4242
<dependency>
4343
<groupId>cn.jpush.api</groupId>
4444
<artifactId>jiguang-common</artifactId>
45-
<version>1.0.7</version>
45+
<version>1.0.8</version>
4646
</dependency>
4747
<dependency>
4848
<groupId>org.apache.httpcomponents</groupId>

src/main/java/cn/jpush/api/JPushClient.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Set;
55

66
import cn.jpush.api.push.CIDResult;
7+
import cn.jpush.api.report.*;
8+
import cn.jpush.api.report.model.CheckMessagePayload;
79
import com.google.gson.JsonObject;
810

911
import cn.jiguang.common.ClientConfig;
@@ -29,10 +31,6 @@
2931
import cn.jpush.api.push.model.audience.Audience;
3032
import cn.jpush.api.push.model.notification.IosAlert;
3133
import cn.jpush.api.push.model.notification.Notification;
32-
import cn.jpush.api.report.MessagesResult;
33-
import cn.jpush.api.report.ReceivedsResult;
34-
import cn.jpush.api.report.ReportClient;
35-
import cn.jpush.api.report.UsersResult;
3634
import cn.jpush.api.schedule.ScheduleClient;
3735
import cn.jpush.api.schedule.ScheduleListResult;
3836
import cn.jpush.api.schedule.ScheduleResult;
@@ -261,6 +259,11 @@ public UsersResult getReportUsers(TimeUnit timeUnit, String start, int duration)
261259
public MessagesResult getReportMessages(String msgIds) throws APIConnectionException, APIRequestException {
262260
return _reportClient.getMessages(msgIds);
263261
}
262+
263+
public Map<String, MessageStatus> getMessageStatus(CheckMessagePayload payload)
264+
throws APIConnectionException, APIRequestException {
265+
return _reportClient.getMessagesStatus(payload);
266+
}
264267

265268

266269
// ------------------------------ Shortcuts - notification
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.jpush.api.report;
2+
3+
import java.io.Serializable;
4+
5+
public class MessageStatus implements Serializable {
6+
7+
/**
8+
* 0 送达
9+
* 1 未送达
10+
* 2 用户不属于该 app
11+
* 3 用户属于该 app,但不属于该 msgid
12+
* 4 系统异常
13+
*/
14+
private int status;
15+
16+
public void setStatus(int status) {
17+
this.status = status;
18+
}
19+
20+
public int getStatus() {
21+
return status;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "status : " + status;
27+
}
28+
}

src/main/java/cn/jpush/api/report/ReportClient.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cn.jpush.api.report;
22

33

4+
import java.lang.reflect.Type;
45
import java.net.URLEncoder;
6+
import java.util.Map;
57
import java.util.regex.Pattern;
68

79
import cn.jiguang.common.ClientConfig;
@@ -14,6 +16,9 @@
1416
import cn.jiguang.common.resp.APIRequestException;
1517
import cn.jiguang.common.resp.BaseResult;
1618
import cn.jiguang.common.resp.ResponseWrapper;
19+
import cn.jpush.api.report.model.CheckMessagePayload;
20+
import com.google.gson.Gson;
21+
import com.google.gson.reflect.TypeToken;
1722

1823
public class ReportClient {
1924

@@ -100,6 +105,14 @@ public MessagesResult getMessages(String msgIds)
100105

101106
return MessagesResult.fromResponse(response);
102107
}
108+
109+
public Map<String, MessageStatus> getMessagesStatus(CheckMessagePayload payload)
110+
throws APIConnectionException, APIRequestException {
111+
String url = _hostName + "/v3/status/message";
112+
ResponseWrapper result = _httpClient.sendPost(url, payload.toString());
113+
Type type = new TypeToken<Map<String, MessageStatus>>(){}.getType();
114+
return new Gson().fromJson(result.responseContent, type);
115+
}
103116

104117
public UsersResult getUsers(TimeUnit timeUnit, String start, int duration)
105118
throws APIConnectionException, APIRequestException {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package cn.jpush.api.report.model;
2+
3+
import cn.jiguang.common.utils.Preconditions;
4+
import cn.jiguang.common.utils.TimeUtils;
5+
import cn.jpush.api.schedule.model.IModel;
6+
import com.google.gson.*;
7+
8+
import java.text.ParseException;
9+
import java.text.SimpleDateFormat;
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.Collections;
13+
import java.util.List;
14+
15+
public class CheckMessagePayload implements IModel{
16+
17+
public final static String MSG_ID = "msg_id";
18+
public final static String REGISTRATION_IDS = "registration_ids";
19+
public final static String DATE = "date";
20+
21+
private long msgId = -1L;
22+
private List<String> registrationIds;
23+
private String date;
24+
private Gson gson = new Gson();
25+
public CheckMessagePayload(long msgId, List<String> rids, String date) {
26+
this.msgId = msgId;
27+
this.registrationIds = rids;
28+
this.date = date;
29+
}
30+
31+
public static Builder newBuilder() {
32+
return new Builder();
33+
}
34+
35+
public static class Builder {
36+
private long msgId;
37+
private List<String> registrationIds = new ArrayList<String>();
38+
private String date;
39+
40+
public Builder setMsgId(long msgId) {
41+
this.msgId = msgId;
42+
return this;
43+
}
44+
45+
public Builder setRegistrationIds(String[] rids) {
46+
Preconditions.checkArgument(rids != null && rids.length > 0, "Registration ids is empty");
47+
Collections.addAll(registrationIds, rids);
48+
return this;
49+
}
50+
51+
public Builder setRegistrationsIds(List<String> rids) {
52+
Preconditions.checkArgument(rids != null && rids.size() > 0, "Registration ids is empty");
53+
registrationIds = rids;
54+
return this;
55+
}
56+
57+
public Builder addRegistrationIds(String... rids) {
58+
if (null == rids) {
59+
return this;
60+
}
61+
Collections.addAll(registrationIds, rids);
62+
return this;
63+
}
64+
65+
public Builder addRegistrationIds(List<String> rids) {
66+
registrationIds.addAll(rids);
67+
return this;
68+
}
69+
70+
public Builder setDate(String date) {
71+
Preconditions.checkArgument(isDayFormat(date), "Date format is invalid");
72+
this.date = date;
73+
return this;
74+
}
75+
76+
public CheckMessagePayload build() {
77+
return new CheckMessagePayload(msgId, registrationIds, date);
78+
}
79+
}
80+
81+
@Override
82+
public JsonElement toJSON() {
83+
JsonObject jsonObject = new JsonObject();
84+
if (msgId != -1L) {
85+
jsonObject.addProperty(MSG_ID, msgId);
86+
}
87+
if (null != registrationIds) {
88+
JsonArray jsonArray = new JsonArray();
89+
for (String rid : registrationIds) {
90+
jsonArray.add(new JsonPrimitive(rid));
91+
}
92+
jsonObject.add(REGISTRATION_IDS, jsonArray);
93+
}
94+
if (null != date) {
95+
jsonObject.addProperty(DATE, date);
96+
}
97+
return jsonObject;
98+
}
99+
100+
@Override
101+
public String toString() {
102+
return gson.toJson(toJSON());
103+
}
104+
105+
private static boolean isDayFormat(String date) {
106+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
107+
try {
108+
simpleDateFormat.parse(date);
109+
return true;
110+
} catch (ParseException e) {
111+
e.printStackTrace();
112+
return false;
113+
}
114+
}
115+
}

src/test/java/cn/jpush/api/BaseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public abstract class BaseTest {
66

77
protected static final String APP_KEY = "d4ee2375846bc30fa51334f5";
8-
protected static final String MASTER_SECRET = "61807e56ddaebf2e47172159";
8+
protected static final String MASTER_SECRET = "f88f9b289c3f681eef0b95ee";
99
protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e";
1010
protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c";
1111

src/test/java/cn/jpush/api/push/PushClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void test_validate() {
131131
public void testGetCidList() {
132132
JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
133133
try {
134-
CIDResult result = jPushClient.getCidList(3, null);
134+
CIDResult result = jPushClient.getCidList(3, "push");
135135
LOG.info("Got result - " + result);
136136
} catch (APIConnectionException e) {
137137
LOG.error("Connection error. Should retry later. ", e);

0 commit comments

Comments
 (0)