Skip to content

Commit cbe96c6

Browse files
committed
更新了notification中安卓缺失的参数, 更新了对notification_3rd的支持 v3.4.13
1 parent 5d1a59b commit cbe96c6

File tree

6 files changed

+485
-69
lines changed

6 files changed

+485
-69
lines changed

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

+27-12
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public class PushExample {
3232
protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);
3333

3434
// demo App defined in resources/jpush-api.conf
35-
protected static final String APP_KEY = "8f02a4fa717a6235734d92de";
36-
protected static final String MASTER_SECRET = "cf6de29f9e66432ba4ac1c32";
35+
protected static final String APP_KEY = "7d431e42dfa6a6d693ac2d04";
36+
protected static final String MASTER_SECRET = "5e987ac6d2e04d95a9d8f0d1";
3737
protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c";
3838
protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e";
3939

@@ -56,7 +56,7 @@ public static void main(String[] args) {
5656
testSendPush();
5757
// testGetCidList();
5858
// testSendPushes();
59-
// testSendPush_fromJSON();
59+
testSendPush_fromJSON();
6060
// testSendPushWithCallback();
6161
// testSendPushWithCid();
6262
}
@@ -280,15 +280,30 @@ public static PushPayload buildPushObject_android_and_ios() {
280280
return PushPayload.newBuilder()
281281
.setPlatform(Platform.android_ios())
282282
.setAudience(Audience.all())
283-
.setNotification(Notification.newBuilder()
284-
.setAiOpportunity(false)
285-
.setAlert("testing alert content")
286-
.addPlatformNotification(AndroidNotification.newBuilder()
287-
.setTitle("Android Title")
288-
.addExtras(extras).build())
289-
.addPlatformNotification(IosNotification.newBuilder()
290-
.incrBadge(1)
291-
.addExtra("extra_key", "extra_value").build())
283+
.setMessage(Message.newBuilder()
284+
.setMsgContent("Hi, JPush")
285+
.build())
286+
// .setNotification(Notification.newBuilder()
287+
// .setAiOpportunity(false)
288+
// .setAlert("testing alert content")
289+
// .addPlatformNotification(AndroidNotification.newBuilder()
290+
// .setTitle("Android Title")
291+
// .addExtras(extras).build())
292+
// .addPlatformNotification(IosNotification.newBuilder()
293+
// .incrBadge(1)
294+
// .addExtra("extra_key", "extra_value").build())
295+
// .build())
296+
.setNotification3rd(Notification3rd.newBuilder()
297+
.setContent("Hi, JPush")
298+
.setTitle("msg testing")
299+
.setChannelId("channel1001")
300+
.setUriActivity("cn.jpush.android.ui.OpenClickActivity")
301+
.setUriAction("cn.jpush.android.intent.CONNECTION")
302+
.setBadgeAddNum(1)
303+
.setBadgeClass("com.test.badge.MainActivity")
304+
.setSound("sound")
305+
.addExtra("news_id", 124)
306+
.addExtra("my_key", "a value")
292307
.build())
293308
.setOptions(Options.newBuilder()
294309
.setApnsProduction(false)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
package cn.jpush.api.push.model;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import com.google.gson.JsonElement;
7+
import com.google.gson.JsonNull;
8+
import com.google.gson.JsonObject;
9+
import com.google.gson.JsonPrimitive;
10+
11+
import cn.jiguang.common.utils.Preconditions;
12+
13+
14+
public class Notification3rd implements PushModel{
15+
private static final String TITLE = "title";
16+
private static final String CONTENT = "content";
17+
private static final String CHANNEL_ID = "channel_id";
18+
private static final String URI_ACTIVITY = "uri_activity";
19+
private static final String URI_ACTION = "uri_action";
20+
private static final String BADGE_ADD_NUM = "badge_add_num";
21+
private static final String BADGE_CLASS = "badge_class";
22+
private static final String SOUND = "sound";
23+
private static final String EXTRAS = "extras";
24+
25+
26+
private final String title;
27+
private final String content;
28+
private final String channel_id;
29+
private final String uri_activity;
30+
private final String uri_action;
31+
private final int badge_add_num;
32+
private final String badge_class;
33+
private final String sound;
34+
private final Map<String, String> extras;
35+
private final Map<String, Number> numberExtras;
36+
private final Map<String, Boolean> booleanExtras;
37+
private final Map<String, JsonObject> jsonExtras;
38+
39+
private Notification3rd(String title, String content, String channel_id,
40+
String uri_activity, String uri_action, int badge_add_num,
41+
String badge_class, String sound,
42+
Map<String, String> extras,
43+
Map<String, Number> numberExtras,
44+
Map<String, Boolean> booleanExtras,
45+
Map<String, JsonObject> jsonExtras) {
46+
this.title = title;
47+
this.content = content;
48+
this.channel_id = channel_id;
49+
this.uri_activity = uri_activity;
50+
this.uri_action = uri_action;
51+
this.badge_add_num = badge_add_num;
52+
this.badge_class = badge_class;
53+
this.sound = sound;
54+
this.extras = extras;
55+
this.numberExtras = numberExtras;
56+
this.booleanExtras = booleanExtras;
57+
this.jsonExtras = jsonExtras;
58+
}
59+
60+
61+
public static Builder newBuilder() { return new Builder(); }
62+
63+
@Override
64+
public JsonElement toJSON() {
65+
JsonObject json = new JsonObject();
66+
67+
if (null != title) {
68+
json.addProperty("title", title);
69+
}
70+
71+
//如果必填,该怎么判定
72+
json.addProperty("content", content);
73+
74+
if (null != channel_id) {
75+
json.addProperty("channel_id", channel_id);
76+
}
77+
78+
if (null != uri_activity) {
79+
json.addProperty("uri_activity", uri_activity);
80+
}
81+
82+
if (null != uri_action) {
83+
json.addProperty("uri_action", uri_action);
84+
}
85+
86+
if (0 != badge_add_num) {
87+
json.addProperty("badge_add_num", badge_add_num);
88+
}
89+
90+
if (null != badge_class) {
91+
json.addProperty("badge_class", badge_class);
92+
}
93+
94+
if (null != sound) {
95+
json.addProperty("sound", sound);
96+
}
97+
98+
JsonObject extrasObject = null;
99+
if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) {
100+
extrasObject = new JsonObject();
101+
}
102+
103+
if (null != extras) {
104+
String value = null;
105+
for (String key : extras.keySet()) {
106+
value = extras.get(key);
107+
if (null != value) {
108+
extrasObject.add(key, new JsonPrimitive(value));
109+
}
110+
}
111+
}
112+
if (null != numberExtras) {
113+
Number value = null;
114+
for (String key : numberExtras.keySet()) {
115+
value = numberExtras.get(key);
116+
if (null != value) {
117+
extrasObject.add(key, new JsonPrimitive(value));
118+
}
119+
}
120+
}
121+
if (null != booleanExtras) {
122+
Boolean value = null;
123+
for (String key : booleanExtras.keySet()) {
124+
value = booleanExtras.get(key);
125+
if (null != value) {
126+
extrasObject.add(key, new JsonPrimitive(value));
127+
}
128+
}
129+
}
130+
if (null != jsonExtras) {
131+
JsonObject value = null;
132+
for (String key : jsonExtras.keySet()) {
133+
value = jsonExtras.get(key);
134+
if (null != value) {
135+
extrasObject.add(key, value);
136+
}
137+
}
138+
}
139+
140+
if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) {
141+
json.add("extras", extrasObject);
142+
}
143+
144+
return json;
145+
}
146+
147+
public static class Builder{
148+
private String title;
149+
private String content;
150+
private String channel_id;
151+
private String uri_activity;
152+
private String uri_action;
153+
private int badge_add_num;
154+
private String badge_class;
155+
private String sound;
156+
protected Map<String, String> extrasBuilder;
157+
protected Map<String, Number> numberExtrasBuilder;
158+
protected Map<String, Boolean> booleanExtrasBuilder;
159+
protected Map<String, JsonObject> jsonExtrasBuilder;
160+
161+
public Builder setTitle(String title) {
162+
this.title = title;
163+
return this;
164+
}
165+
166+
public Builder setContent(String content) {
167+
this.content = content;
168+
return this;
169+
}
170+
171+
public Builder setChannelId(String channel_id) {
172+
this.channel_id = channel_id;
173+
return this;
174+
}
175+
176+
public Builder setUriActivity(String uri_activity) {
177+
this.uri_activity = uri_activity;
178+
return this;
179+
}
180+
181+
public Builder setUriAction(String uri_action) {
182+
this.uri_action = uri_action;
183+
return this;
184+
}
185+
186+
public Builder setBadgeAddNum(int badge_add_num) {
187+
this.badge_add_num = badge_add_num;
188+
return this;
189+
}
190+
191+
public Builder setBadgeClass(String badge_class) {
192+
this.badge_class = badge_class;
193+
return this;
194+
}
195+
196+
public Builder setSound(String sound) {
197+
this.sound = sound;
198+
return this;
199+
}
200+
201+
public Builder addExtra(String key, String value) {
202+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
203+
if (null == extrasBuilder) {
204+
extrasBuilder = new HashMap<String, String>();
205+
}
206+
extrasBuilder.put(key, value);
207+
return this;
208+
}
209+
210+
public Builder addExtra(Map<String, String> extras) {
211+
Preconditions.checkArgument(! (null == extras), "extras should not be null.");
212+
if (null == extrasBuilder) {
213+
extrasBuilder = new HashMap<String, String>();
214+
}
215+
for (String key : extras.keySet()) {
216+
extrasBuilder.put(key, extras.get(key));
217+
}
218+
return this;
219+
}
220+
221+
public Builder addExtra(String key, Number value) {
222+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
223+
if (null == numberExtrasBuilder) {
224+
numberExtrasBuilder = new HashMap<String, Number>();
225+
}
226+
numberExtrasBuilder.put(key, value);
227+
return this;
228+
}
229+
230+
public Builder addExtra(String key, Boolean value) {
231+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
232+
if (null == booleanExtrasBuilder) {
233+
booleanExtrasBuilder = new HashMap<String, Boolean>();
234+
}
235+
booleanExtrasBuilder.put(key, value);
236+
return this;
237+
}
238+
239+
public Builder addExtra(String key, JsonObject value) {
240+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
241+
if (null == jsonExtrasBuilder) {
242+
jsonExtrasBuilder = new HashMap<String, JsonObject>();
243+
}
244+
jsonExtrasBuilder.put(key, value);
245+
return this;
246+
}
247+
248+
public Notification3rd build() {
249+
Preconditions.checkArgument(content != null && content != "", "content should not be null or empty");
250+
251+
return new Notification3rd(title, content, channel_id, uri_activity, uri_action, badge_add_num,
252+
badge_class, sound, extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder);
253+
}
254+
}
255+
}

src/main/java/cn/jpush/api/push/model/PushPayload.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class PushPayload implements PushModel {
3535
private static final String MESSAGE = "message";
3636
private static final String OPTIONS = "options";
3737
private static final String SMS = "sms_message";
38+
private static final String NOTIFICATION3RD = "notification_3rd";
3839
private static final String CID = "cid";
3940
private static final String TARGET = "target";
4041

@@ -48,7 +49,7 @@ public class PushPayload implements PushModel {
4849
*/
4950
private static final int MAX_IOS_PAYLOAD_LENGTH = 2000;
5051

51-
private static Gson _gson = new GsonBuilder().disableHtmlEscaping().create();
52+
private static Gson _gson = new GsonBuilder().disableHtmlEscaping().create();
5253

5354
private final Platform platform;
5455
private final Audience audience;
@@ -57,19 +58,22 @@ public class PushPayload implements PushModel {
5758
private final Message message;
5859
private Options options;
5960
private SMS sms;
61+
private final Notification3rd notification3rd;
6062
private String cid;
6163
private String target;
6264
protected Map<String, JsonObject> custom;
6365

6466
private PushPayload(Platform platform, Audience audience,
65-
Notification notification, InappMessage inappMessage, Message message, Options options, SMS sms, String cid, String target, Map<String, JsonObject> custom) {
67+
Notification notification, InappMessage inappMessage, Message message, Options options,
68+
SMS sms, Notification3rd notification3rd, String cid, String target, Map<String, JsonObject> custom) {
6669
this.platform = platform;
6770
this.audience = audience;
6871
this.notification = notification;
6972
this.inappMessage = inappMessage;
7073
this.message = message;
7174
this.options = options;
7275
this.sms = sms;
76+
this.notification3rd = notification3rd;
7377
this.cid = cid;
7478
this.target = target;
7579
this.custom = custom;
@@ -201,6 +205,9 @@ public JsonElement toJSON() {
201205
if (null != sms) {
202206
json.add(SMS, sms.toJSON());
203207
}
208+
if (null != notification3rd) {
209+
json.add(NOTIFICATION3RD, notification3rd.toJSON());
210+
}
204211
if (null != cid) {
205212
json.addProperty(CID, cid);
206213
}
@@ -271,6 +278,7 @@ public static class Builder {
271278
private Message message = null;
272279
private Options options = null;
273280
private SMS sms = null;
281+
private Notification3rd notification3rd = null;
274282
private String cid;
275283
private String target;
276284
private Map<String, JsonObject> custom;
@@ -319,6 +327,11 @@ public Builder setSMS(SMS sms) {
319327
return this;
320328
}
321329

330+
public Builder setNotification3rd(Notification3rd notification3rd) {
331+
this.notification3rd = notification3rd;
332+
return this;
333+
}
334+
322335
public Builder setCid(String cid) {
323336
this.cid = cid;
324337
return this;
@@ -348,7 +361,7 @@ public PushPayload build() {
348361
options = Options.sendno();
349362
}
350363

351-
return new PushPayload(platform, audience, notification, inappMessage, message, options, sms, cid, target, custom);
364+
return new PushPayload(platform, audience, notification, inappMessage, message, options, sms, notification3rd, cid, target, custom);
352365
}
353366
}
354367
}

0 commit comments

Comments
 (0)