Skip to content

Commit d611608

Browse files
committed
Merge action buttons
2 parents 8d7e892 + f6a093a commit d611608

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ Notification notification = pushpad.buildNotification("Title", "Message", "http:
7474
notification.iconUrl = "http://example.com/assets/icon.png";
7575
// optional, drop the notification after this number of seconds if a device is offline
7676
notification.ttl = 604800;
77+
// optional, add some action buttons to the notification
78+
// see https://pushpad.xyz/docs/action_buttons
79+
ActionButton button1 = new ActionButton("My Button 1"); // Title (max length is 20 characters)
80+
button1.targetUrl = "http://example.com/button-link"; // optional
81+
button1.icon = "http://example.com/assets/button-icon.png"; // optional
82+
button1.action = "myActionName"; // optional
83+
notification.actionButtons = new ActionButton[]{button1};
7784

7885
try {
7986
// deliver the notification to a user

src/xyz/pushpad/ActionButton.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package xyz.pushpad;
2+
3+
import org.json.simple.JSONObject;
4+
5+
public class ActionButton {
6+
public String title;
7+
public String targetUrl;
8+
public String icon;
9+
public String action;
10+
11+
public ActionButton(String title) {
12+
this.title = title;
13+
}
14+
15+
public JSONObject toJson() {
16+
JSONObject result = new JSONObject();
17+
result.put("title", title);
18+
if (targetUrl != null) {
19+
result.put("target_url", targetUrl);
20+
}
21+
if (icon != null) {
22+
result.put("icon", icon);
23+
}
24+
if (action != null) {
25+
result.put("action", action);
26+
}
27+
return result;
28+
}
29+
}

src/xyz/pushpad/Notification.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class Notification {
1515
public String targetUrl;
1616
public String iconUrl;
1717
public Integer ttl;
18+
public ActionButton[] actionButtons;
1819

1920
public Notification(Pushpad pushpad, String title, String body, String targetUrl) {
2021
this.pushpad = pushpad;
@@ -60,6 +61,13 @@ private String reqBody(String[] uids, String[] tags) {
6061
if (this.ttl != null) {
6162
notificationData.put("ttl", this.ttl);
6263
}
64+
if (actionButtons != null) {
65+
JSONArray jsonActionButtons = new JSONArray();
66+
for (ActionButton actionButton : actionButtons) {
67+
jsonActionButtons.add(actionButton.toJson());
68+
}
69+
notificationData.put("actions", jsonActionButtons);
70+
}
6371
body.put("notification", notificationData);
6472
if (uids != null) {
6573
JSONArray jsonUids = new JSONArray();

0 commit comments

Comments
 (0)