Skip to content

Commit c1ec720

Browse files
authored
Merge pull request #99 from sswapnnil/master
Added two new fields relevance score and interruption level which are added in ios-15
2 parents 45ea01e + 9a34aef commit c1ec720

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

src/main/java/com/clevertap/apns/Notification.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
package com.clevertap.apns;
3232

3333
import com.clevertap.apns.clients.AsyncOkHttpApnsClient;
34+
import com.clevertap.apns.enums.InterruptionLevel;
3435
import com.fasterxml.jackson.core.JsonProcessingException;
3536
import com.fasterxml.jackson.databind.ObjectMapper;
3637

@@ -40,6 +41,8 @@
4041

4142
/**
4243
* An entity containing the payload and the token.
44+
* <br>
45+
* See <a href="https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification">here</a> for more information.
4346
*/
4447
public class Notification {
4548
private final String payload;
@@ -272,6 +275,35 @@ public Builder pushType(String pushType) {
272275
return this;
273276
}
274277

278+
/**
279+
* Sets the relevance score for this notification.
280+
* @param score A value between 0 and 1, both inclusive
281+
*/
282+
public Builder relevanceScore(double score) {
283+
if (score >= 0 && score <= 1){
284+
aps.put("relevance-score", score);
285+
}
286+
return this;
287+
}
288+
289+
public Builder resetRelevanceScore() {
290+
aps.remove("relevance-score");
291+
return this;
292+
}
293+
294+
public Builder resetInterruptionLevel() {
295+
aps.remove("interruption-level");
296+
return this;
297+
}
298+
299+
public Builder interruptionLevel(InterruptionLevel interruptionLevel) {
300+
if (interruptionLevel != null) {
301+
aps.put("interruption-level", interruptionLevel.getValue());
302+
}
303+
return this;
304+
}
305+
306+
275307
public int size() {
276308
try {
277309
return build().getPayload().getBytes("UTF-8").length;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.clevertap.apns.enums;
2+
3+
public enum InterruptionLevel {
4+
5+
ACTIVE("active"),
6+
PASSIVE("passive"),
7+
TIME_SENSITIVE("time-sensitive"),
8+
CRITICAL("critical")
9+
;
10+
11+
12+
private final String value;
13+
14+
InterruptionLevel(String value) {
15+
this.value = value;
16+
}
17+
18+
public String getValue() {
19+
return value;
20+
}
21+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.clevertap.apns;
2+
3+
import com.clevertap.apns.enums.InterruptionLevel;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
9+
import java.util.stream.Stream;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
class NotificationTest {
14+
15+
@Test
16+
void testNotificationBuilder() {
17+
Notification.Builder builder = new Notification.Builder("token");
18+
builder.relevanceScore(0.1);
19+
builder.interruptionLevel(InterruptionLevel.PASSIVE);
20+
builder.mutableContent(true);
21+
builder.alertBody("body");
22+
builder.alertTitle("title");
23+
builder.category("cat1");
24+
builder.priority(Notification.Priority.IMMEDIATE);
25+
Notification notification = builder.build();
26+
assertEquals("{\"aps\":{\"interruption-level\":\"passive\",\"relevance-score\":0.1,\"alert\":{\"body\":\"body\",\"title\":\"title\"},\"category\":\"cat1\",\"mutable-content\":1}}", notification.getPayload());
27+
}
28+
29+
@Test
30+
void testNotificationBuilderWithResetOptions() {
31+
Notification.Builder builder = new Notification.Builder("token");
32+
builder.relevanceScore(0.1);
33+
builder.interruptionLevel(InterruptionLevel.PASSIVE);
34+
builder.mutableContent(true);
35+
builder.alertBody("body");
36+
builder.alertTitle("title");
37+
builder.category("cat1");
38+
builder.priority(Notification.Priority.IMMEDIATE);
39+
builder.resetRelevanceScore();
40+
builder.resetInterruptionLevel();
41+
Notification notification = builder.build();
42+
assertEquals("{\"aps\":{\"alert\":{\"body\":\"body\",\"title\":\"title\"},\"category\":\"cat1\",\"mutable-content\":1}}", notification.getPayload());
43+
}
44+
45+
46+
@ParameterizedTest
47+
@MethodSource("providerForRelevanceScore")
48+
void testRelevanceScore(String expected, double relevanceScore) {
49+
Notification.Builder builder = new Notification.Builder("token");
50+
builder.relevanceScore(relevanceScore);
51+
assertEquals(expected, builder.build().getPayload());
52+
}
53+
54+
static Stream<Arguments> providerForRelevanceScore() {
55+
Stream.Builder<Arguments> builder = Stream.builder();
56+
builder.add(Arguments.of("{\"aps\":{\"relevance-score\":0.0,\"alert\":{}}}", 0.0));
57+
builder.add(Arguments.of("{\"aps\":{\"relevance-score\":1.0,\"alert\":{}}}", 1.0));
58+
builder.add(Arguments.of("{\"aps\":{\"relevance-score\":0.75,\"alert\":{}}}", 0.75));
59+
builder.add(Arguments.of("{\"aps\":{\"alert\":{}}}", 5.0));
60+
builder.add(Arguments.of("{\"aps\":{\"alert\":{}}}", -1.0));
61+
return builder.build();
62+
}
63+
64+
@ParameterizedTest
65+
@MethodSource("providerInterruptionLevel")
66+
void testInterruptionLevel(String expected, InterruptionLevel interruptionLevel) {
67+
Notification.Builder builder = new Notification.Builder("token");
68+
builder.interruptionLevel(interruptionLevel);
69+
assertEquals(expected, builder.build().getPayload());
70+
}
71+
72+
static Stream<Arguments> providerInterruptionLevel() {
73+
Stream.Builder<Arguments> builder = Stream.builder();
74+
75+
String payloadFormat = "{\"aps\":{\"interruption-level\":\"%s\",\"alert\":{}}}";
76+
for (InterruptionLevel interruptionLevel: InterruptionLevel.values()) {
77+
builder.add(Arguments.of(String.format(payloadFormat, interruptionLevel.getValue()), interruptionLevel));
78+
}
79+
builder.add(Arguments.of("{\"aps\":{\"alert\":{}}}", null));
80+
return builder.build();
81+
}
82+
83+
@Test
84+
void resetRelevanceScore() {
85+
Notification.Builder builder = new Notification.Builder("token");
86+
builder.relevanceScore(0.5);
87+
Notification notification = builder.build();
88+
assertEquals("{\"aps\":{\"relevance-score\":0.5,\"alert\":{}}}", notification.getPayload());
89+
90+
builder.resetRelevanceScore();
91+
Notification notification2 = builder.build();
92+
assertEquals("{\"aps\":{\"alert\":{}}}", notification2.getPayload());
93+
}
94+
95+
@Test
96+
void resetInterruptionLevel() {
97+
Notification.Builder builder = new Notification.Builder("token");
98+
builder.interruptionLevel(InterruptionLevel.ACTIVE);
99+
Notification notification = builder.build();
100+
assertEquals("{\"aps\":{\"interruption-level\":\"active\",\"alert\":{}}}", notification.getPayload());
101+
102+
builder.resetInterruptionLevel();
103+
Notification notification2 = builder.build();
104+
assertEquals("{\"aps\":{\"alert\":{}}}", notification2.getPayload());
105+
}
106+
107+
}

0 commit comments

Comments
 (0)