Skip to content

Commit 088439c

Browse files
author
Javen
committed
Add feature: ios notification category support.
1 parent 9746e1f commit 088439c

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/main/java/cn/jpush/api/push/model/notification/IosNotification.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class IosNotification extends PlatformNotification {
3535
private static final String BADGE = "badge";
3636
private static final String SOUND = "sound";
3737
private static final String CONTENT_AVAILABLE = "content-available";
38+
private static final String CATEGORY = "category";
3839

3940
private static final String ALERT_VALID_BADGE = "Badge number should be 0~99999, "
4041
+ "and can be prefixed with + to add, - to minus";
@@ -45,9 +46,11 @@ public class IosNotification extends PlatformNotification {
4546
private final String sound;
4647
private final String badge;
4748
private final boolean contentAvailable;
49+
private final String category;
4850

4951
private IosNotification(String alert, String sound, String badge,
5052
boolean contentAvailable, boolean soundDisabled, boolean badgeDisabled,
53+
String category,
5154
Map<String, String> extras,
5255
Map<String, Number> numberExtras,
5356
Map<String, Boolean> booleanExtras,
@@ -59,6 +62,7 @@ private IosNotification(String alert, String sound, String badge,
5962
this.contentAvailable = contentAvailable;
6063
this.soundDisabled = soundDisabled;
6164
this.badgeDisabled = badgeDisabled;
65+
this.category = category;
6266
}
6367

6468
public static Builder newBuilder() {
@@ -96,6 +100,9 @@ public JsonElement toJSON() {
96100
if (contentAvailable) {
97101
json.add(CONTENT_AVAILABLE, new JsonPrimitive(1));
98102
}
103+
if (null != category) {
104+
json.add(CATEGORY, new JsonPrimitive(category));
105+
}
99106

100107
return json;
101108
}
@@ -107,6 +114,7 @@ public static class Builder extends PlatformNotification.Builder<IosNotification
107114
private boolean contentAvailable = false;
108115
private boolean soundDisabled = false;
109116
private boolean badgeDisabled = false;
117+
private String category;
110118

111119
public Builder setSound(String sound) {
112120
this.sound = sound;
@@ -158,6 +166,11 @@ public Builder setContentAvailable(boolean contentAvailable) {
158166
return this;
159167
}
160168

169+
public Builder setCategory(String category) {
170+
this.category = category;
171+
return this;
172+
}
173+
161174
public Builder setAlert(String alert) {
162175
this.alert = alert;
163176
return this;
@@ -233,7 +246,7 @@ public Builder addExtra(String key, JsonObject value) {
233246

234247
public IosNotification build() {
235248
return new IosNotification(alert, sound, badge, contentAvailable,
236-
soundDisabled, badgeDisabled,
249+
soundDisabled, badgeDisabled, category,
237250
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder);
238251
}
239252
}

src/test/java/cn/jpush/api/push/model/notification/IosNotificationTest.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package cn.jpush.api.push.model.notification;
22

3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertThat;
6+
37
import org.junit.Assert;
48
import org.junit.Test;
59

10+
import com.google.gson.JsonElement;
611
import com.google.gson.JsonObject;
712
import com.google.gson.JsonPrimitive;
813

@@ -14,7 +19,7 @@ public void testEmpty() {
1419
JsonObject json = new JsonObject();
1520
json.add("sound", new JsonPrimitive(""));
1621
json.add("badge", new JsonPrimitive("+1"));
17-
Assert.assertEquals("", json, ios.toJSON());
22+
assertEquals("", json, ios.toJSON());
1823
}
1924

2025
@Test
@@ -24,7 +29,7 @@ public void testQuickAlert() {
2429
json.add("alert", new JsonPrimitive("aaa"));
2530
json.add("sound", new JsonPrimitive(""));
2631
json.add("badge", new JsonPrimitive("+1"));
27-
Assert.assertEquals("", json, ios.toJSON());
32+
assertEquals("", json, ios.toJSON());
2833
}
2934

3035
@Test
@@ -33,7 +38,7 @@ public void testBadge_0() {
3338
JsonObject json = new JsonObject();
3439
json.add("badge", new JsonPrimitive("0"));
3540
json.add("sound", new JsonPrimitive(""));
36-
Assert.assertEquals("", json, ios.toJSON());
41+
assertEquals("", json, ios.toJSON());
3742
}
3843

3944
@Test
@@ -42,7 +47,7 @@ public void testBadge_2() {
4247
JsonObject json = new JsonObject();
4348
json.add("badge", new JsonPrimitive("2"));
4449
json.add("sound", new JsonPrimitive(""));
45-
Assert.assertEquals("", json, ios.toJSON());
50+
assertEquals("", json, ios.toJSON());
4651
}
4752

4853
@Test
@@ -51,7 +56,7 @@ public void testBadge_auto() {
5156
JsonObject json = new JsonObject();
5257
json.add("badge", new JsonPrimitive("+1"));
5358
json.add("sound", new JsonPrimitive(""));
54-
Assert.assertEquals("", json, ios.toJSON());
59+
assertEquals("", json, ios.toJSON());
5560
}
5661

5762
@Test
@@ -60,7 +65,7 @@ public void testBadge_plus_2() {
6065
JsonObject json = new JsonObject();
6166
json.add("badge", new JsonPrimitive("+2"));
6267
json.add("sound", new JsonPrimitive(""));
63-
Assert.assertEquals("", json, ios.toJSON());
68+
assertEquals("", json, ios.toJSON());
6469
}
6570

6671
@Test
@@ -69,7 +74,7 @@ public void testBadge_plus_0() {
6974
JsonObject json = new JsonObject();
7075
json.add("badge", new JsonPrimitive("+0"));
7176
json.add("sound", new JsonPrimitive(""));
72-
Assert.assertEquals("", json, ios.toJSON());
77+
assertEquals("", json, ios.toJSON());
7378
}
7479

7580
@Test
@@ -78,7 +83,7 @@ public void testBadge_minus_2() {
7883
JsonObject json = new JsonObject();
7984
json.add("badge", new JsonPrimitive("-2"));
8085
json.add("sound", new JsonPrimitive(""));
81-
Assert.assertEquals("", json, ios.toJSON());
86+
assertEquals("", json, ios.toJSON());
8287
}
8388

8489
@Test
@@ -100,7 +105,7 @@ public void testSoundDisabled() {
100105
JsonObject json = new JsonObject();
101106
json.add("alert", new JsonPrimitive("alert"));
102107
json.add("badge", new JsonPrimitive("+1"));
103-
Assert.assertEquals("", json, ios.toJSON());
108+
assertEquals("", json, ios.toJSON());
104109
}
105110

106111
@Test
@@ -112,7 +117,7 @@ public void testBadgeDisabled() {
112117
JsonObject json = new JsonObject();
113118
json.add("alert", new JsonPrimitive("alert"));
114119
json.add("sound", new JsonPrimitive(""));
115-
Assert.assertEquals("", json, ios.toJSON());
120+
assertEquals("", json, ios.toJSON());
116121
}
117122

118123

@@ -128,10 +133,21 @@ public void testExtra() {
128133
json.add("extras", extra);
129134
json.add("sound", new JsonPrimitive(""));
130135
json.add("badge", new JsonPrimitive("+1"));
131-
Assert.assertEquals("", json, ios.toJSON());
136+
assertEquals("", json, ios.toJSON());
132137
}
133138

134-
139+
@Test
140+
public void testCategory() {
141+
IosNotification ios = IosNotification.newBuilder()
142+
.setCategory("java").build();
143+
144+
JsonObject json = new JsonObject();
145+
json.add("category", new JsonPrimitive("java"));
146+
json.add("sound", new JsonPrimitive(""));
147+
json.add("badge", new JsonPrimitive("+1"));
148+
149+
assertThat(ios.toJSON(), is((JsonElement) json));
150+
}
135151
}
136152

137153

0 commit comments

Comments
 (0)