From e758e201dab8299527c9097511e98089a6c038fc Mon Sep 17 00:00:00 2001 From: Daniel Heid Date: Thu, 28 May 2020 15:25:08 +0200 Subject: [PATCH] Add more custom alert fields --- README.md | 1 + .../notification/PushNotificationPayload.java | 79 +++++++++++++++- .../PushNotificationPayloadTest.java | 93 ++++++++++++++++++- 3 files changed, 165 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c6795e1..0dd75c3 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ This project is licensed under the LGPL License - see the [license](LICENSE) fil * Logging improvements (replace string concatenation) * Add more aps dictionary items: thread-id, category and target-content-id +* Add more custom alert fields: title, subtitle, launch-image, title-loc-key, title-loc-args, subtitle-loc-key, subtitle-loc-args ### 2.4.1 diff --git a/src/main/java/javapns/notification/PushNotificationPayload.java b/src/main/java/javapns/notification/PushNotificationPayload.java index 847b2a1..279a00e 100644 --- a/src/main/java/javapns/notification/PushNotificationPayload.java +++ b/src/main/java/javapns/notification/PushNotificationPayload.java @@ -6,6 +6,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Collection; import java.util.IllegalFormatException; import java.util.List; @@ -359,6 +360,76 @@ public void addCustomAlertBody(String body) { put("body", body, getOrAddCustomAlert(), false); } + /** + * Create a custom alert (if none exist) and add a title to the custom alert. + * + * @param title the title of the alert + * @throws JSONException if the custom alert cannot be added because a simple alert already exists + */ + public void addCustomAlertTitle(String title) { + put("title", title, getOrAddCustomAlert(), false); + } + + /** + * Create a custom alert (if none exist) and add a subtitle to the custom alert. + * + * @param subtitle the subtitle of the alert + * @throws JSONException if the custom alert cannot be added because a simple alert already exists + */ + public void addCustomAlertSubtitle(String subtitle) { + put("subtitle", subtitle, getOrAddCustomAlert(), false); + } + + /** + * Create a custom alert (if none exist) and add a launch image to the custom alert. + * + * @param launchImage the subtitle of the alert + * @throws JSONException if the custom alert cannot be added because a simple alert already exists + */ + public void addCustomAlertLaunchImage(String launchImage) { + put("launch-image", launchImage, getOrAddCustomAlert(), false); + } + + /** + * Create a custom alert (if none exist) and add a key for a localized title string to the custom alert. + * + * @param titleLocKey the key for a localized title string of the alert + * @throws JSONException if the custom alert cannot be added because a simple alert already exists + */ + public void addCustomAlertTitleLocKey(String titleLocKey) { + put("title-loc-key", titleLocKey, getOrAddCustomAlert(), false); + } + + /** + * 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. + * + * @param titleLocArgs the array of strings containing replacement values for variables in your title string of the alert + * @throws JSONException if the custom alert cannot be added because a simple alert already exists + */ + public void addCustomAlertTitleLocArgs(Collection titleLocArgs) { + put("title-loc-args", titleLocArgs, getOrAddCustomAlert(), false); + } + + /** + * Create a custom alert (if none exist) and add a key for a localized title string to the custom alert. + * + * @param subtitleLocKey the key for a localized subtitle string of the alert + * @throws JSONException if the custom alert cannot be added because a simple alert already exists + */ + public void addCustomAlertSubtitleLocKey(String subtitleLocKey) { + put("subtitle-loc-key", subtitleLocKey, getOrAddCustomAlert(), false); + } + + /** + * 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. + * + * @param subtitleLocArgs the array of strings containing replacement values for variables in your title string of the alert + * @throws JSONException if the custom alert cannot be added because a simple alert already exists + */ + public void addCustomAlertSubtitleLocArgs(Collection subtitleLocArgs) { + put("subtitle-loc-args", subtitleLocArgs, getOrAddCustomAlert(), false); + } + /** * Create a custom alert (if none exist) and add a custom text for the right button of the popup. * @@ -366,7 +437,7 @@ public void addCustomAlertBody(String body) { * @throws JSONException if the custom alert cannot be added because a simple alert already exists */ public void addCustomAlertActionLocKey(String actionLocKey) { - Object value = actionLocKey != null ? actionLocKey : JSONObject.NULL; + Object value = actionLocKey == null ? JSONObject.NULL : actionLocKey; put("action-loc-key", value, getOrAddCustomAlert(), false); } @@ -383,11 +454,11 @@ public void addCustomAlertLocKey(String locKey) { /** * Create a custom alert (if none exist) and add sub-parameters for the loc-key parameter. * - * @param args The loc-args parameter + * @param locArgs The loc-args * @throws JSONException if the custom alert cannot be added because a simple alert already exists */ - public void addCustomAlertLocArgs(List args) { - put("loc-args", args, getOrAddCustomAlert(), false); + public void addCustomAlertLocArgs(Collection locArgs) { + put("loc-args", locArgs, getOrAddCustomAlert(), false); } /** diff --git a/src/test/java/javapns/notification/PushNotificationPayloadTest.java b/src/test/java/javapns/notification/PushNotificationPayloadTest.java index f278d65..e7d4944 100644 --- a/src/test/java/javapns/notification/PushNotificationPayloadTest.java +++ b/src/test/java/javapns/notification/PushNotificationPayloadTest.java @@ -1,11 +1,12 @@ package javapns.notification; -import org.json.JSONObject; -import org.junit.Test; - +import static java.util.Collections.singleton; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import org.json.JSONObject; +import org.junit.Test; + public class PushNotificationPayloadTest { @@ -53,7 +54,7 @@ public void allowsToAddCategory() { } @Test - public void allowsToTargetContentId() { + public void allowsToAddTargetContentId() { pushNotificationPayload.addTargetContentId("myTargetContentId"); @@ -63,4 +64,88 @@ public void allowsToTargetContentId() { } + @Test + public void allowsToSetCustomAlertTitle() { + + pushNotificationPayload.addCustomAlertTitle("customAlertTitle"); + + JSONObject payload = pushNotificationPayload.getPayload(); + JSONObject aps = payload.getJSONObject("aps"); + JSONObject alert = aps.getJSONObject("alert"); + assertThat(alert.getString("title"), is("customAlertTitle")); + + } + + @Test + public void allowsToSetCustomAlertSubtitle() { + + pushNotificationPayload.addCustomAlertSubtitle("customAlertSubTitle"); + + JSONObject payload = pushNotificationPayload.getPayload(); + JSONObject aps = payload.getJSONObject("aps"); + JSONObject alert = aps.getJSONObject("alert"); + assertThat(alert.getString("subtitle"), is("customAlertSubTitle")); + + } + + @Test + public void allowsToSetCustomAlertLaunchImage() { + + pushNotificationPayload.addCustomAlertLaunchImage("customAlertLaunchImage"); + + JSONObject payload = pushNotificationPayload.getPayload(); + JSONObject aps = payload.getJSONObject("aps"); + JSONObject alert = aps.getJSONObject("alert"); + assertThat(alert.getString("launch-image"), is("customAlertLaunchImage")); + + } + + @Test + public void allowsToSetCustomAlertTitleLocKey() { + + pushNotificationPayload.addCustomAlertTitleLocKey("customAlertTitleLocKey"); + + JSONObject payload = pushNotificationPayload.getPayload(); + JSONObject aps = payload.getJSONObject("aps"); + JSONObject alert = aps.getJSONObject("alert"); + assertThat(alert.getString("title-loc-key"), is("customAlertTitleLocKey")); + + } + + @Test + public void allowsToSetCustomAlertTitleLocArgs() { + + pushNotificationPayload.addCustomAlertTitleLocArgs(singleton("customAlertTitleLocArgs")); + + JSONObject payload = pushNotificationPayload.getPayload(); + JSONObject aps = payload.getJSONObject("aps"); + JSONObject alert = aps.getJSONObject("alert"); + assertThat(alert.get("title-loc-args"), is(singleton("customAlertTitleLocArgs"))); + + } + + @Test + public void allowsToSetCustomAlertSubtitleLocKey() { + + pushNotificationPayload.addCustomAlertSubtitleLocKey("customAlertSubtitleLocKey"); + + JSONObject payload = pushNotificationPayload.getPayload(); + JSONObject aps = payload.getJSONObject("aps"); + JSONObject alert = aps.getJSONObject("alert"); + assertThat(alert.getString("subtitle-loc-key"), is("customAlertSubtitleLocKey")); + + } + + @Test + public void allowsToSetCustomAlertSubtitleLocArgs() { + + pushNotificationPayload.addCustomAlertSubtitleLocArgs(singleton("customAlertSubtitleLocArgs")); + + JSONObject payload = pushNotificationPayload.getPayload(); + JSONObject aps = payload.getJSONObject("aps"); + JSONObject alert = aps.getJSONObject("alert"); + assertThat(alert.get("subtitle-loc-args"), is(singleton("customAlertSubtitleLocArgs"))); + + } + }