Skip to content

Commit 0c8d1d8

Browse files
committed
notification中增加voip参数支持自定消息
sms增加active_filter参数 v3.5.1
1 parent a5cb44d commit 0c8d1d8

File tree

7 files changed

+276
-64
lines changed

7 files changed

+276
-64
lines changed

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

Lines changed: 43 additions & 28 deletions
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 = "7d431e42dfa6a6d693ac2d04";
36-
protected static final String MASTER_SECRET = "5e987ac6d2e04d95a9d8f0d1";
35+
protected static final String APP_KEY = "8f02a4fa717a6235734d92de";
36+
protected static final String MASTER_SECRET = "cf6de29f9e66432ba4ac1c32";
3737
protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c";
3838
protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e";
3939

@@ -53,12 +53,15 @@ public static void main(String[] args) {
5353
// testBatchSend();
5454
// testSendPushWithCustomConfig();
5555
// testSendIosAlert();
56+
57+
// 目前推荐这个方法进行测试
5658
testSendPush();
5759
// testGetCidList();
5860
// testSendPushes();
59-
testSendPush_fromJSON();
61+
// testSendPush_fromJSON();
6062
// testSendPushWithCallback();
61-
// testSendPushWithCid();
63+
// testSendPushWithCid();
64+
// testSendWithSMS();
6265
}
6366

6467
// 使用 NettyHttpClient 异步接口发送请求
@@ -92,8 +95,10 @@ public static void testSendPush() {
9295
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
9396
// NettyHttpClient httpClient =new NettyHttpClient(authCode, null, clientConfig);
9497
// jpushClient.getPushClient().setHttpClient(httpClient);
98+
99+
// For push, all you need do is to build PushPayload object.
95100
final PushPayload payload = buildPushObject_android_and_ios();
96-
// // For push, all you need do is to build PushPayload object.
101+
97102
// PushPayload payload = buildPushObject_all_alias_alert();
98103
try {
99104
PushResult result = jpushClient.sendPush(payload);
@@ -277,34 +282,43 @@ public static PushPayload buildPushObject_android_tag_alertWithTitle() {
277282
public static PushPayload buildPushObject_android_and_ios() {
278283
Map<String, String> extras = new HashMap<String, String>();
279284
extras.put("test", "https://community.jiguang.cn/push");
285+
// you can set anything you want in this builder, read the document to avoid collision.
280286
return PushPayload.newBuilder()
281287
.setPlatform(Platform.android_ios())
282288
.setAudience(Audience.all())
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())
289+
// .setMessage(Message.newBuilder()
290+
// .setMsgContent("Hi, JPush")
295291
// .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")
292+
.setNotification(Notification.newBuilder()
293+
.setAlert("testing alert content")
294+
.addPlatformNotification(AndroidNotification.newBuilder()
295+
.setTitle("Android Title")
296+
.addExtras(extras).build())
297+
.addPlatformNotification(IosNotification.newBuilder()
298+
.incrBadge(1)
299+
.addExtra("extra_key", "extra_value").build())
300+
.addVoip("key", "value")
301+
.addVoip("key2", 2)
302+
.addVoip("key3", true)
307303
.build())
304+
// .setSMS(SMS.newBuilder()
305+
// .setDelayTime(1000)
306+
// .setTempID(2000)
307+
// .addPara("Test", 1)
308+
// .setActiveFilter(true)
309+
// .build())
310+
// .setNotification3rd(Notification3rd.newBuilder()
311+
// .setContent("Hi, JPush")
312+
// .setTitle("msg testing")
313+
// .setChannelId("channel1001")
314+
// .setUriActivity("cn.jpush.android.ui.OpenClickActivity")
315+
// .setUriAction("cn.jpush.android.intent.CONNECTION")
316+
// .setBadgeAddNum(1)
317+
// .setBadgeClass("com.test.badge.MainActivity")
318+
// .setSound("sound")
319+
// .addExtra("news_id", 124)
320+
// .addExtra("my_key", "a value")
321+
// .build())
308322
.setOptions(Options.newBuilder()
309323
.setApnsProduction(false)
310324
.setTimeToLive(43200)
@@ -501,6 +515,7 @@ public static void testSendWithSMS() {
501515
.setDelayTime(1000)
502516
.setTempID(2000)
503517
.addPara("Test", 1)
518+
.setActiveFilter(false)
504519
.build();
505520
PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "alias1");
506521
LOG.info("Got result - " + result);

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

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

33
import cn.jiguang.common.utils.Preconditions;
44

5-
import cn.jpush.api.push.model.notification.Notification;
65
import com.google.gson.JsonElement;
76
import com.google.gson.JsonObject;
87

9-
8+
/**
9+
* inapp_message 此功能生效需Android push SDK≥V3.9.0、iOS push SDK≥V3.4.0,若低于此版本按照原流程执行。
10+
*
11+
* 默认值为false
12+
*/
1013
public class InappMessage implements PushModel{
1114

1215
private boolean inappMessage;
@@ -15,11 +18,13 @@ private InappMessage(boolean inappMessage) {
1518
this.inappMessage = inappMessage;
1619
}
1720

21+
/**
22+
* the entrance for building a inappMessage object
23+
* @return inappMessage builder
24+
*/
1825
public static Builder newBuilder() { return new Builder(); }
1926

20-
public static InappMessage inappMessage(boolean inappMessage) {
21-
return newBuilder().setInappMessage(inappMessage).build();
22-
}
27+
public boolean getInappMessage() { return inappMessage; }
2328

2429

2530
@Override
@@ -35,6 +40,7 @@ public JsonElement toJSON() {
3540
public static class Builder {
3641
private boolean inappMessage;
3742

43+
3844
public Builder setInappMessage(boolean inappMessage) {
3945
this.inappMessage = inappMessage;
4046
return this;

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@
1111

1212
import cn.jiguang.common.utils.Preconditions;
1313

14+
/**
15+
* Android 1.6.2 及以下版本 接收 notification 与 message 并存(即本次 api 调用同时推送通知和消息)的离线推送, 只能收到通知部分,message 部分没有透传给 App。
16+
*
17+
* Android 1.6.3 及以上 SDK 版本已做相应调整,能正常接收同时推送通知和消息的离线记录。
18+
*
19+
* iOS 1.7.3 及以上的版本才能正确解析 v3 的 message,但是无法解析 v2 推送通知同时下发的应用内消息。
20+
*/
1421
public class Message implements PushModel {
15-
private static final String TITLE = "title";
1622
private static final String MSG_CONTENT = "msg_content";
23+
private static final String TITLE = "title";
1724
private static final String CONTENT_TYPE = "content_type";
1825
private static final String EXTRAS = "extras";
1926

20-
private final String title;
2127
private final String msgContent;
28+
private final String title;
2229
private final String contentType;
2330
private final Map<String, String> extras;
2431
private final Map<String, Number> numberExtras;
@@ -106,6 +113,7 @@ public JsonElement toJSON() {
106113
return json;
107114
}
108115

116+
109117
public static class Builder {
110118
private String title;
111119
private String msgContent;

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010

1111
import cn.jiguang.common.utils.Preconditions;
1212

13-
13+
/**
14+
* 使用说明
15+
* notification_3rd 只针对开通了厂商通道的用户生效;
16+
* notification 和 notification_3rd 不能同时有内容,如果这两块同时有内容,则会返回错误提示;
17+
* notification_3rd 的内容对 iOS 和 WinPhone 平台无效,只针对 Android 平台生效;
18+
* notification_3rd 是用作补发厂商通知的内容,只有当 message 部分有内容,才允许传递此字段,且要两者都不为空时,才会对离线的厂商设备转发厂商通道的通知。
19+
*/
1420
public class Notification3rd implements PushModel{
1521
private static final String TITLE = "title";
1622
private static final String CONTENT = "content";
@@ -58,43 +64,50 @@ private Notification3rd(String title, String content, String channel_id,
5864
}
5965

6066

67+
/**
68+
* The entrance for building a Notification3rd object.
69+
* @return Notification3rd builder
70+
*/
6171
public static Builder newBuilder() { return new Builder(); }
6272

6373
@Override
6474
public JsonElement toJSON() {
6575
JsonObject json = new JsonObject();
6676

6777
if (null != title) {
68-
json.addProperty("title", title);
78+
json.addProperty(TITLE, title);
6979
}
7080

71-
//如果必填,该怎么判定
72-
json.addProperty("content", content);
81+
// 必填
82+
json.addProperty(CONTENT, content);
7383

7484
if (null != channel_id) {
75-
json.addProperty("channel_id", channel_id);
85+
json.addProperty(CHANNEL_ID, channel_id);
7686
}
7787

7888
if (null != uri_activity) {
79-
json.addProperty("uri_activity", uri_activity);
89+
json.addProperty(URI_ACTIVITY, uri_activity);
8090
}
8191

8292
if (null != uri_action) {
83-
json.addProperty("uri_action", uri_action);
93+
json.addProperty(URI_ACTION, uri_action);
8494
}
8595

8696
if (0 != badge_add_num) {
87-
json.addProperty("badge_add_num", badge_add_num);
97+
json.addProperty(BADGE_ADD_NUM, badge_add_num);
8898
}
8999

90100
if (null != badge_class) {
91-
json.addProperty("badge_class", badge_class);
101+
json.addProperty(BADGE_CLASS, badge_class);
92102
}
93103

94104
if (null != sound) {
95-
json.addProperty("sound", sound);
105+
json.addProperty(SOUND, sound);
96106
}
97107

108+
/**
109+
* for adding extras into json
110+
*/
98111
JsonObject extrasObject = null;
99112
if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) {
100113
extrasObject = new JsonObject();
@@ -138,7 +151,7 @@ public JsonElement toJSON() {
138151
}
139152

140153
if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) {
141-
json.add("extras", extrasObject);
154+
json.add(EXTRAS, extrasObject);
142155
}
143156

144157
return json;
@@ -198,6 +211,7 @@ public Builder setSound(String sound) {
198211
return this;
199212
}
200213

214+
// addExtra 一次加入一对
201215
public Builder addExtra(String key, String value) {
202216
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
203217
if (null == extrasBuilder) {
@@ -207,7 +221,8 @@ public Builder addExtra(String key, String value) {
207221
return this;
208222
}
209223

210-
public Builder addExtra(Map<String, String> extras) {
224+
// addExtras 可以一次加入多对
225+
public Builder addExtras(Map<String, String> extras) {
211226
Preconditions.checkArgument(! (null == extras), "extras should not be null.");
212227
if (null == extrasBuilder) {
213228
extrasBuilder = new HashMap<String, String>();

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,32 @@
1010

1111
import cn.jiguang.common.utils.Preconditions;
1212

13+
/**
14+
* sms_message 用于设置短信推送内容以及短信发送的延迟时间。
15+
*/
1316
public class SMS implements PushModel {
1417

1518
private final String content;
1619
private final int delay_time;
1720
private final long temp_id;
21+
// default is true
22+
private boolean active_filter;
23+
// this flag is used to indicate if the active_filter being set
24+
private boolean is_set_active_filter = false;
1825
private final Map<String, String> extras;
1926
private final Map<String, Number> numberExtras;
2027
private final Map<String, Boolean> booleanExtras;
2128
private final Map<String, JsonObject> jsonExtras;
2229

23-
private SMS(String content, int delay_time, long temp_id,
30+
private SMS(String content, int delay_time, long temp_id, boolean active_filter,
2431
Map<String, String> extras,
2532
Map<String, Number> numberExtras,
2633
Map<String, Boolean> booleanExtras,
2734
Map<String, JsonObject> jsonExtras) {
2835
this.content = content;
2936
this.delay_time = delay_time;
3037
this.temp_id = temp_id;
38+
this.active_filter = active_filter;
3139
this.extras = extras;
3240
this.numberExtras = numberExtras;
3341
this.booleanExtras = booleanExtras;
@@ -80,9 +88,11 @@ public JsonElement toJSON() {
8088
json.addProperty("content", content);
8189
}
8290

91+
json.addProperty("active_filter", active_filter);
92+
8393

8494
JsonObject extrasObject = null;
85-
if (null != extras || null != numberExtras || null != booleanExtras) {
95+
if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) {
8696
extrasObject = new JsonObject();
8797
}
8898

@@ -111,7 +121,7 @@ public JsonElement toJSON() {
111121
}
112122
}
113123

114-
if (null != extras || null != numberExtras || null != booleanExtras) {
124+
if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) {
115125
json.add("temp_para", extrasObject);
116126
}
117127
return json;
@@ -121,6 +131,8 @@ public static class Builder {
121131
private String content;
122132
private int delay_time;
123133
private long temp_id;
134+
private boolean active_filter;
135+
private boolean is_set_active_filter;
124136
private Map<String, String> extrasBuilder;
125137
private Map<String, Number> numberExtrasBuilder;
126138
private Map<String, Boolean> booleanExtrasBuilder;
@@ -140,6 +152,12 @@ public Builder setTempID(long tempID) {
140152
this.temp_id = tempID;
141153
return this;
142154
}
155+
156+
public Builder setActiveFilter(boolean activeFilter) {
157+
this.active_filter = activeFilter;
158+
this.is_set_active_filter = true;
159+
return this;
160+
}
143161

144162
public Builder addPara(String key, String value) {
145163
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
@@ -191,7 +209,10 @@ public Builder addPara(String key, JsonObject value) {
191209
public SMS build() {
192210
Preconditions.checkArgument(delay_time >= 0, "The delay time must be greater than or equal to 0");
193211

194-
return new SMS(content, delay_time, temp_id,
212+
// if active filter not being set, will default set it to true.
213+
if (is_set_active_filter == false) { active_filter = true; }
214+
215+
return new SMS(content, delay_time, temp_id, active_filter,
195216
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder,jsonExtrasBuilder);
196217
}
197218

0 commit comments

Comments
 (0)