Skip to content

Commit 3032498

Browse files
committed
Add scheduled notifications
1 parent e16326a commit 3032498

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ button1.action = "myActionName"; // optional
9090
notification.actionButtons = new ActionButton[]{button1};
9191
// optional, bookmark the notification in the Pushpad dashboard (e.g. to highlight manual notifications)
9292
notification.starred = true;
93+
// optional, use this option only if you need to create scheduled notifications (max 5 days)
94+
// see https://pushpad.xyz/docs/schedule_notifications
95+
Instant tomorrow = Instant.now().plusSeconds(60*60*24);
96+
notification.sendAt = tomorrow;
9397

9498
try {
9599
// deliver the notification to a user
@@ -132,6 +136,7 @@ The methods above return a JSONObject:
132136
- `res.get("id")` is the id of the notification on Pushpad
133137
- `res.get("scheduled")` is the estimated reach of the notification (i.e. the number of devices to which the notification will be sent, which can be different from the number of users, since a user may receive notifications on multiple devices)
134138
- `res.get("uids")` (`deliverTo` only) are the user IDs that will be actually reached by the notification because they are subscribed to your notifications. For example if you send a notification to `{"uid1", "uid2", "uid3"}`, but only `"uid1"` is subscribed, you will get `{"uid1"}` in response. Note that if a user has unsubscribed after the last notification sent to him, he may still be reported for one time as subscribed (this is due to the way the W3C Push API works).
139+
- `res.get("send_at")` is present only for scheduled notifications. The fields `scheduled` and `uids` are not available in this case.
135140

136141
## License
137142

src/xyz/pushpad/Notification.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.*;
88
import java.net.URL;
99
import javax.net.ssl.HttpsURLConnection;
10+
import java.time.Instant;
1011

1112
public class Notification {
1213
public Pushpad pushpad;
@@ -20,6 +21,7 @@ public class Notification {
2021
public String customData;
2122
public ActionButton[] actionButtons;
2223
public Boolean starred;
24+
public Instant sendAt;
2325

2426
public Notification(Pushpad pushpad, String title, String body, String targetUrl) {
2527
this.pushpad = pushpad;
@@ -84,6 +86,9 @@ private String reqBody(String[] uids, String[] tags) {
8486
if (this.starred != null) {
8587
notificationData.put("starred", this.starred);
8688
}
89+
if (this.sendAt != null) {
90+
notificationData.put("send_at", this.sendAt.toString());
91+
}
8792
body.put("notification", notificationData);
8893
if (uids != null) {
8994
JSONArray jsonUids = new JSONArray();

0 commit comments

Comments
 (0)