Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit e758e20

Browse files
author
Daniel Heid
committed
Add more custom alert fields
1 parent 256962e commit e758e20

File tree

3 files changed

+165
-8
lines changed

3 files changed

+165
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ This project is licensed under the LGPL License - see the [license](LICENSE) fil
4949

5050
* Logging improvements (replace string concatenation)
5151
* Add more aps dictionary items: thread-id, category and target-content-id
52+
* Add more custom alert fields: title, subtitle, launch-image, title-loc-key, title-loc-args, subtitle-loc-key, subtitle-loc-args
5253

5354
### 2.4.1
5455

src/main/java/javapns/notification/PushNotificationPayload.java

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88

9+
import java.util.Collection;
910
import java.util.IllegalFormatException;
1011
import java.util.List;
1112

@@ -359,14 +360,84 @@ public void addCustomAlertBody(String body) {
359360
put("body", body, getOrAddCustomAlert(), false);
360361
}
361362

363+
/**
364+
* Create a custom alert (if none exist) and add a title to the custom alert.
365+
*
366+
* @param title the title of the alert
367+
* @throws JSONException if the custom alert cannot be added because a simple alert already exists
368+
*/
369+
public void addCustomAlertTitle(String title) {
370+
put("title", title, getOrAddCustomAlert(), false);
371+
}
372+
373+
/**
374+
* Create a custom alert (if none exist) and add a subtitle to the custom alert.
375+
*
376+
* @param subtitle the subtitle of the alert
377+
* @throws JSONException if the custom alert cannot be added because a simple alert already exists
378+
*/
379+
public void addCustomAlertSubtitle(String subtitle) {
380+
put("subtitle", subtitle, getOrAddCustomAlert(), false);
381+
}
382+
383+
/**
384+
* Create a custom alert (if none exist) and add a launch image to the custom alert.
385+
*
386+
* @param launchImage the subtitle of the alert
387+
* @throws JSONException if the custom alert cannot be added because a simple alert already exists
388+
*/
389+
public void addCustomAlertLaunchImage(String launchImage) {
390+
put("launch-image", launchImage, getOrAddCustomAlert(), false);
391+
}
392+
393+
/**
394+
* Create a custom alert (if none exist) and add a key for a localized title string to the custom alert.
395+
*
396+
* @param titleLocKey the key for a localized title string of the alert
397+
* @throws JSONException if the custom alert cannot be added because a simple alert already exists
398+
*/
399+
public void addCustomAlertTitleLocKey(String titleLocKey) {
400+
put("title-loc-key", titleLocKey, getOrAddCustomAlert(), false);
401+
}
402+
403+
/**
404+
* Create a custom alert (if none exist) and add a array of strings containing replacement values for variables in your title string to the custom alert.
405+
*
406+
* @param titleLocArgs the array of strings containing replacement values for variables in your title string of the alert
407+
* @throws JSONException if the custom alert cannot be added because a simple alert already exists
408+
*/
409+
public void addCustomAlertTitleLocArgs(Collection<CharSequence> titleLocArgs) {
410+
put("title-loc-args", titleLocArgs, getOrAddCustomAlert(), false);
411+
}
412+
413+
/**
414+
* Create a custom alert (if none exist) and add a key for a localized title string to the custom alert.
415+
*
416+
* @param subtitleLocKey the key for a localized subtitle string of the alert
417+
* @throws JSONException if the custom alert cannot be added because a simple alert already exists
418+
*/
419+
public void addCustomAlertSubtitleLocKey(String subtitleLocKey) {
420+
put("subtitle-loc-key", subtitleLocKey, getOrAddCustomAlert(), false);
421+
}
422+
423+
/**
424+
* Create a custom alert (if none exist) and add a array of strings containing replacement values for variables in your subtitle string to the custom alert.
425+
*
426+
* @param subtitleLocArgs the array of strings containing replacement values for variables in your title string of the alert
427+
* @throws JSONException if the custom alert cannot be added because a simple alert already exists
428+
*/
429+
public void addCustomAlertSubtitleLocArgs(Collection<CharSequence> subtitleLocArgs) {
430+
put("subtitle-loc-args", subtitleLocArgs, getOrAddCustomAlert(), false);
431+
}
432+
362433
/**
363434
* Create a custom alert (if none exist) and add a custom text for the right button of the popup.
364435
*
365436
* @param actionLocKey the title of the alert's right button, or null to remove the button
366437
* @throws JSONException if the custom alert cannot be added because a simple alert already exists
367438
*/
368439
public void addCustomAlertActionLocKey(String actionLocKey) {
369-
Object value = actionLocKey != null ? actionLocKey : JSONObject.NULL;
440+
Object value = actionLocKey == null ? JSONObject.NULL : actionLocKey;
370441
put("action-loc-key", value, getOrAddCustomAlert(), false);
371442
}
372443

@@ -383,11 +454,11 @@ public void addCustomAlertLocKey(String locKey) {
383454
/**
384455
* Create a custom alert (if none exist) and add sub-parameters for the loc-key parameter.
385456
*
386-
* @param args The loc-args parameter
457+
* @param locArgs The loc-args
387458
* @throws JSONException if the custom alert cannot be added because a simple alert already exists
388459
*/
389-
public void addCustomAlertLocArgs(List<?> args) {
390-
put("loc-args", args, getOrAddCustomAlert(), false);
460+
public void addCustomAlertLocArgs(Collection<CharSequence> locArgs) {
461+
put("loc-args", locArgs, getOrAddCustomAlert(), false);
391462
}
392463

393464
/**

src/test/java/javapns/notification/PushNotificationPayloadTest.java

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package javapns.notification;
22

3-
import org.json.JSONObject;
4-
import org.junit.Test;
5-
3+
import static java.util.Collections.singleton;
64
import static org.hamcrest.CoreMatchers.is;
75
import static org.hamcrest.MatcherAssert.assertThat;
86

7+
import org.json.JSONObject;
8+
import org.junit.Test;
9+
910

1011
public class PushNotificationPayloadTest {
1112

@@ -53,7 +54,7 @@ public void allowsToAddCategory() {
5354
}
5455

5556
@Test
56-
public void allowsToTargetContentId() {
57+
public void allowsToAddTargetContentId() {
5758

5859
pushNotificationPayload.addTargetContentId("myTargetContentId");
5960

@@ -63,4 +64,88 @@ public void allowsToTargetContentId() {
6364

6465
}
6566

67+
@Test
68+
public void allowsToSetCustomAlertTitle() {
69+
70+
pushNotificationPayload.addCustomAlertTitle("customAlertTitle");
71+
72+
JSONObject payload = pushNotificationPayload.getPayload();
73+
JSONObject aps = payload.getJSONObject("aps");
74+
JSONObject alert = aps.getJSONObject("alert");
75+
assertThat(alert.getString("title"), is("customAlertTitle"));
76+
77+
}
78+
79+
@Test
80+
public void allowsToSetCustomAlertSubtitle() {
81+
82+
pushNotificationPayload.addCustomAlertSubtitle("customAlertSubTitle");
83+
84+
JSONObject payload = pushNotificationPayload.getPayload();
85+
JSONObject aps = payload.getJSONObject("aps");
86+
JSONObject alert = aps.getJSONObject("alert");
87+
assertThat(alert.getString("subtitle"), is("customAlertSubTitle"));
88+
89+
}
90+
91+
@Test
92+
public void allowsToSetCustomAlertLaunchImage() {
93+
94+
pushNotificationPayload.addCustomAlertLaunchImage("customAlertLaunchImage");
95+
96+
JSONObject payload = pushNotificationPayload.getPayload();
97+
JSONObject aps = payload.getJSONObject("aps");
98+
JSONObject alert = aps.getJSONObject("alert");
99+
assertThat(alert.getString("launch-image"), is("customAlertLaunchImage"));
100+
101+
}
102+
103+
@Test
104+
public void allowsToSetCustomAlertTitleLocKey() {
105+
106+
pushNotificationPayload.addCustomAlertTitleLocKey("customAlertTitleLocKey");
107+
108+
JSONObject payload = pushNotificationPayload.getPayload();
109+
JSONObject aps = payload.getJSONObject("aps");
110+
JSONObject alert = aps.getJSONObject("alert");
111+
assertThat(alert.getString("title-loc-key"), is("customAlertTitleLocKey"));
112+
113+
}
114+
115+
@Test
116+
public void allowsToSetCustomAlertTitleLocArgs() {
117+
118+
pushNotificationPayload.addCustomAlertTitleLocArgs(singleton("customAlertTitleLocArgs"));
119+
120+
JSONObject payload = pushNotificationPayload.getPayload();
121+
JSONObject aps = payload.getJSONObject("aps");
122+
JSONObject alert = aps.getJSONObject("alert");
123+
assertThat(alert.get("title-loc-args"), is(singleton("customAlertTitleLocArgs")));
124+
125+
}
126+
127+
@Test
128+
public void allowsToSetCustomAlertSubtitleLocKey() {
129+
130+
pushNotificationPayload.addCustomAlertSubtitleLocKey("customAlertSubtitleLocKey");
131+
132+
JSONObject payload = pushNotificationPayload.getPayload();
133+
JSONObject aps = payload.getJSONObject("aps");
134+
JSONObject alert = aps.getJSONObject("alert");
135+
assertThat(alert.getString("subtitle-loc-key"), is("customAlertSubtitleLocKey"));
136+
137+
}
138+
139+
@Test
140+
public void allowsToSetCustomAlertSubtitleLocArgs() {
141+
142+
pushNotificationPayload.addCustomAlertSubtitleLocArgs(singleton("customAlertSubtitleLocArgs"));
143+
144+
JSONObject payload = pushNotificationPayload.getPayload();
145+
JSONObject aps = payload.getJSONObject("aps");
146+
JSONObject alert = aps.getJSONObject("alert");
147+
assertThat(alert.get("subtitle-loc-args"), is(singleton("customAlertSubtitleLocArgs")));
148+
149+
}
150+
66151
}

0 commit comments

Comments
 (0)