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

Commit

Permalink
Add more custom alert fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Heid committed May 28, 2020
1 parent 256962e commit e758e20
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
79 changes: 75 additions & 4 deletions src/main/java/javapns/notification/PushNotificationPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.IllegalFormatException;
import java.util.List;

Expand Down Expand Up @@ -359,14 +360,84 @@ 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<CharSequence> 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<CharSequence> 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.
*
* @param actionLocKey the title of the alert's right button, or null to remove the button
* @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);
}

Expand All @@ -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<CharSequence> locArgs) {
put("loc-args", locArgs, getOrAddCustomAlert(), false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -53,7 +54,7 @@ public void allowsToAddCategory() {
}

@Test
public void allowsToTargetContentId() {
public void allowsToAddTargetContentId() {

pushNotificationPayload.addTargetContentId("myTargetContentId");

Expand All @@ -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")));

}

}

0 comments on commit e758e20

Please sign in to comment.