Skip to content

Commit 9e091a6

Browse files
author
Javen
committed
Basically implemented: PushPayload JSON.
1 parent 72a5a0d commit 9e091a6

29 files changed

+1125
-81
lines changed

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,18 @@
7171
<artifactId>slf4j-log4j12</artifactId>
7272
<version>1.7.5</version>
7373
</dependency>
74+
<dependency>
75+
<groupId>com.google.guava</groupId>
76+
<artifactId>guava</artifactId>
77+
<version>12.0</version>
78+
</dependency>
7479

80+
<dependency>
81+
<groupId>joda-time</groupId>
82+
<artifactId>joda-time</artifactId>
83+
<version>2.1</version>
84+
</dependency>
85+
7586
</dependencies>
7687

7788
<build>

src/cn/jpush/api/JPushClient.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import java.util.Map;
44

5-
import cn.jpush.api.common.DeviceEnum;
5+
import cn.jpush.api.common.DeviceType;
66
import cn.jpush.api.push.CustomMessageParams;
77
import cn.jpush.api.push.MessageParams;
88
import cn.jpush.api.push.MessageResult;
99
import cn.jpush.api.push.NotificationParams;
1010
import cn.jpush.api.push.PushClient;
11-
import cn.jpush.api.push.ReceiverTypeEnum;
11+
import cn.jpush.api.push.model.AudienceType;
1212
import cn.jpush.api.report.ReceivedsResult;
1313
import cn.jpush.api.report.ReportClient;
1414

@@ -41,7 +41,7 @@ public JPushClient(String masterSecret, String appKey) {
4141
* @param device The target device of the push. If null will send to all platforms.
4242
* @param apnsProduction If iOS push which environment should be use for sending APNs.
4343
*/
44-
public JPushClient(String masterSecret, String appKey, long timeToLive, DeviceEnum device, boolean apnsProduction) {
44+
public JPushClient(String masterSecret, String appKey, long timeToLive, DeviceType device, boolean apnsProduction) {
4545
_pushClient = new PushClient(masterSecret, appKey, timeToLive, device, apnsProduction);
4646
_reportClient = new ReportClient(masterSecret, appKey);
4747
}
@@ -58,7 +58,7 @@ public MessageResult sendCustomMessage(String msgTitle, String msgContent, Custo
5858

5959
public MessageResult sendCustomMessageAll(String msgTitle, String msgContent) {
6060
CustomMessageParams params = new CustomMessageParams();
61-
params.setReceiverType(ReceiverTypeEnum.APP_KEY);
61+
params.setReceiverType(AudienceType.APP_KEY);
6262
//params.setTimeToLive(MessageParams.DEFAULT_TIME_TO_LIVE);
6363
//params.setSendNo(1);
6464
//params.setOverrideMsgId("");
@@ -67,7 +67,7 @@ public MessageResult sendCustomMessageAll(String msgTitle, String msgContent) {
6767

6868
public MessageResult sendNotificationAll(String notificationContent) {
6969
NotificationParams params = new NotificationParams();
70-
params.setReceiverType(ReceiverTypeEnum.APP_KEY);
70+
params.setReceiverType(AudienceType.APP_KEY);
7171
//params.setTimeToLive(MessageParams.DEFAULT_TIME_TO_LIVE);
7272
//params.setSendNo(1);
7373
//params.setAndroidNotificationTitle("");
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package cn.jpush.api.common;
22

3-
public enum DeviceEnum {
3+
public enum DeviceType {
44

55
Android("android"),
6-
7-
IOS("ios");
6+
IOS("ios"),
7+
MPNs("mpns");
88

99
private final String value;
1010

11-
private DeviceEnum(final String value) {
11+
private DeviceType(final String value) {
1212
this.value = value;
1313
}
1414

src/cn/jpush/api/common/RequestTypeEnum.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/cn/jpush/api/examples/JPushClientExample.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import org.slf4j.LoggerFactory;
55

66
import cn.jpush.api.JPushClient;
7-
import cn.jpush.api.common.DeviceEnum;
7+
import cn.jpush.api.common.DeviceType;
88
import cn.jpush.api.push.MessageResult;
99
import cn.jpush.api.push.NotificationParams;
10-
import cn.jpush.api.push.ReceiverTypeEnum;
10+
import cn.jpush.api.push.model.AudienceType;
1111
import cn.jpush.api.report.ReceivedsResult;
1212

1313
public class JPushClientExample {
@@ -28,11 +28,11 @@ public static void main(String[] args) {
2828
}
2929

3030
private static void testSend() {
31-
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 0, DeviceEnum.Android, false);
31+
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 0, DeviceType.Android, false);
3232
NotificationParams params = new NotificationParams();
3333
//params.setReceiverType(ReceiverTypeEnum.REGISTRATION_ID);
3434
//params.setReceiverValue(registrationID);
35-
params.setReceiverType(ReceiverTypeEnum.TAG);
35+
params.setReceiverType(AudienceType.TAG);
3636
params.setReceiverValue(TAG);
3737

3838
MessageResult msgResult = jpushClient.sendNotification(CONTENT, params, null);

src/cn/jpush/api/push/MessageParams.java

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

6-
import cn.jpush.api.common.DeviceEnum;
6+
import cn.jpush.api.common.DeviceType;
7+
import cn.jpush.api.push.model.AudienceType;
78

89
import com.google.gson.Gson;
910

@@ -37,7 +38,7 @@ public class MessageParams {
3738
/*
3839
* 枚举类定义 ReceiverTypeEnum
3940
*/
40-
private ReceiverTypeEnum receiverType;
41+
private AudienceType receiverType;
4142

4243
/*
4344
* 发送范围值,与 receiverType 相对应。
@@ -60,7 +61,7 @@ public class MessageParams {
6061
/*
6162
* 目标用户中断手机的平台类型,如:android, ios
6263
*/
63-
private Set<DeviceEnum> platform = new HashSet<DeviceEnum>();
64+
private Set<DeviceType> platform = new HashSet<DeviceType>();
6465

6566
// 0: development env 1: production env
6667
private int apnsProduction;
@@ -128,10 +129,10 @@ public String getMasterSecret() {
128129
void setMasterSecret(String masterSecret) {
129130
this.masterSecret = masterSecret;
130131
}
131-
public ReceiverTypeEnum getReceiverType() {
132+
public AudienceType getReceiverType() {
132133
return this.receiverType;
133134
}
134-
public void setReceiverType(ReceiverTypeEnum receiverType) {
135+
public void setReceiverType(AudienceType receiverType) {
135136
this.receiverType = receiverType;
136137
}
137138
public String getReceiverValue() {
@@ -144,12 +145,12 @@ public String getPlatform() {
144145
if (this.platform == null) return "";
145146

146147
String keys = "";
147-
for (DeviceEnum key : this.platform) {
148+
for (DeviceType key : this.platform) {
148149
keys += (key.value() + ",");
149150
}
150151
return keys.length() > 0 ? keys.substring(0, keys.length()-1) : "";
151152
}
152-
public void addPlatform(DeviceEnum platform) {
153+
public void addPlatform(DeviceType platform) {
153154
this.platform.add(platform);
154155
}
155156

src/cn/jpush/api/push/PushClient.java

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

88
import cn.jpush.api.common.BaseHttpClient;
9-
import cn.jpush.api.common.DeviceEnum;
9+
import cn.jpush.api.common.DeviceType;
1010
import cn.jpush.api.common.ResponseResult;
1111
import cn.jpush.api.common.ValidateRequestParams;
1212
import cn.jpush.api.utils.StringUtils;
@@ -21,9 +21,9 @@ public class PushClient extends BaseHttpClient {
2121
private long timeToLive = -1;
2222
private boolean enableSSL = false;
2323
private boolean apnsProduction = false;
24-
private Set<DeviceEnum> devices = new HashSet<DeviceEnum>();
24+
private Set<DeviceType> devices = new HashSet<DeviceType>();
2525

26-
public PushClient(String masterSecret, String appKey, long timeToLive, DeviceEnum device, boolean apnsProduction) {
26+
public PushClient(String masterSecret, String appKey, long timeToLive, DeviceType device, boolean apnsProduction) {
2727
this.masterSecret = masterSecret;
2828
this.appKey = appKey;
2929
this.timeToLive = timeToLive;
@@ -60,7 +60,7 @@ private MessageResult sendMessage(String content, MessageParams params) {
6060
// no specific will then use the setting in instance
6161
params.setTimeToLive(this.timeToLive);
6262
}
63-
for (DeviceEnum device : this.getDevices()) {
63+
for (DeviceType device : this.getDevices()) {
6464
params.addPlatform(device);
6565
}
6666
params.getMsgContent().setMessage(content);
@@ -135,13 +135,13 @@ public String getAppKey() {
135135
return this.appKey;
136136
}
137137

138-
public Set<DeviceEnum> getDevices() {
138+
public Set<DeviceType> getDevices() {
139139
if (null == this.devices) {
140-
this.devices = new HashSet<DeviceEnum>();
140+
this.devices = new HashSet<DeviceType>();
141141
}
142142
if (this.devices.size() == 0) {
143-
this.devices.add(DeviceEnum.Android);
144-
this.devices.add(DeviceEnum.IOS);
143+
this.devices.add(DeviceType.Android);
144+
this.devices.add(DeviceType.IOS);
145145
}
146146
return this.devices;
147147
}

src/cn/jpush/api/push/ReceiverTypeEnum.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package cn.jpush.api.push.model;
2+
3+
import java.util.Map;
4+
5+
import com.google.common.base.Preconditions;
6+
import com.google.common.collect.ImmutableMap;
7+
import com.google.gson.JsonElement;
8+
import com.google.gson.JsonObject;
9+
import com.google.gson.JsonPrimitive;
10+
11+
public class AndroidNotification extends PlatformNotification {
12+
public static final String NOTIFICATION_ANDROID = "android";
13+
14+
public static final String TITLE = "title";
15+
public static final String BUILDER_ID = "builder_id";
16+
public static final String EXTRAS = "extras";
17+
18+
private final String title;
19+
private final int builderId;
20+
private final ImmutableMap<String, String> extras;
21+
22+
private AndroidNotification(String alert, String title, int builderId,
23+
ImmutableMap<String, String> extras) {
24+
super(alert);
25+
this.title = title;
26+
this.builderId = builderId;
27+
this.extras = extras;
28+
}
29+
30+
public static Builder newBuilder() {
31+
return new Builder();
32+
}
33+
34+
public static AndroidNotification alert(String alert) {
35+
return newBuilder().setAlert(alert).build();
36+
}
37+
38+
39+
@Override
40+
public String getPlatform() {
41+
return NOTIFICATION_ANDROID;
42+
}
43+
44+
@Override
45+
public JsonElement toJSON() {
46+
JsonObject json = new JsonObject();
47+
if (null != alert) {
48+
json.add(ALERT, new JsonPrimitive(this.alert));
49+
}
50+
if (builderId > 0) {
51+
json.add(BUILDER_ID, new JsonPrimitive(this.builderId));
52+
}
53+
if (null != title) {
54+
json.add(TITLE, new JsonPrimitive(title));
55+
}
56+
if (null != extras) {
57+
JsonObject extrasObject = new JsonObject();
58+
for (String key : extras.keySet()) {
59+
extrasObject.add(key, new JsonPrimitive(extras.get(key)));
60+
}
61+
json.add(EXTRAS, extrasObject);
62+
}
63+
64+
Preconditions.checkArgument(
65+
! (null == alert && null == title && 0 == builderId && null == extras)
66+
, "No any notification params are set.");
67+
68+
return json;
69+
}
70+
71+
72+
public static class Builder {
73+
private String alert;
74+
private String title;
75+
private int builderId;
76+
private ImmutableMap.Builder<String, String> extrasBuilder;
77+
78+
public Builder setAlert(String alert) {
79+
this.alert = alert;
80+
return this;
81+
}
82+
83+
public Builder setTitle(String title) {
84+
this.title = title;
85+
return this;
86+
}
87+
88+
public Builder setBuilderId(int builderId) {
89+
this.builderId = builderId;
90+
return this;
91+
}
92+
93+
public Builder addExtra(Map<String, String> extra) {
94+
if (null == extrasBuilder) {
95+
extrasBuilder = ImmutableMap.builder();
96+
}
97+
extrasBuilder.putAll(extra);
98+
return this;
99+
}
100+
101+
public Builder addExtra(String key, String value) {
102+
if (null == extrasBuilder) {
103+
extrasBuilder = ImmutableMap.builder();
104+
}
105+
extrasBuilder.put(key, value);
106+
return this;
107+
}
108+
109+
public AndroidNotification build() {
110+
return new AndroidNotification(alert, title, builderId,
111+
(null == extrasBuilder) ? null : extrasBuilder.build());
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)