Skip to content

Commit a3800d6

Browse files
authored
Merge pull request #103 from jamie-nxtgencare/master
Adding support for push notification topics
2 parents 2510b7b + 31d0452 commit a3800d6

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/main/java/nl/martijndwars/webpush/Notification.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ public class Notification {
4040
*/
4141
private Urgency urgency;
4242

43+
/**
44+
* Push Message Topic
45+
*
46+
* @see <a href="https://tools.ietf.org/html/rfc8030#section-5.4">Replacing Push Messages</a>
47+
*
48+
*/
49+
private String topic;
50+
4351
/**
4452
* Time in seconds that the push message is retained by the push service
4553
*/
@@ -48,18 +56,18 @@ public class Notification {
4856
private static final int ONE_DAY_DURATION_IN_SECONDS = 86400;
4957
private static int DEFAULT_TTL = 28 * ONE_DAY_DURATION_IN_SECONDS;
5058

51-
52-
public Notification(String endpoint, ECPublicKey userPublicKey, byte[] userAuth, byte[] payload, int ttl, Urgency urgency) {
59+
public Notification(String endpoint, ECPublicKey userPublicKey, byte[] userAuth, byte[] payload, int ttl, Urgency urgency, String topic) {
5360
this.endpoint = endpoint;
5461
this.userPublicKey = userPublicKey;
5562
this.userAuth = userAuth;
5663
this.payload = payload;
5764
this.ttl = ttl;
5865
this.urgency = urgency;
66+
this.topic = topic;
5967
}
6068

6169
public Notification(String endpoint, PublicKey userPublicKey, byte[] userAuth, byte[] payload, int ttl) {
62-
this(endpoint, (ECPublicKey) userPublicKey, userAuth, payload, ttl, null);
70+
this(endpoint, (ECPublicKey) userPublicKey, userAuth, payload, ttl, null, null);
6371
}
6472

6573
public Notification(String endpoint, String userPublicKey, String userAuth, byte[] payload, int ttl) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
@@ -116,6 +124,10 @@ public boolean hasUrgency() {
116124
return urgency != null;
117125
}
118126

127+
public boolean hasTopic() {
128+
return topic != null;
129+
}
130+
119131
/**
120132
* Detect if the notification is for a GCM-based subscription
121133
*
@@ -137,6 +149,10 @@ public Urgency getUrgency() {
137149
return urgency;
138150
}
139151

152+
public String getTopic() {
153+
return topic;
154+
}
155+
140156
public String getOrigin() throws MalformedURLException {
141157
URL url = new URL(getEndpoint());
142158

@@ -153,12 +169,14 @@ public static class NotificationBuilder {
153169
private byte[] userAuth = null;
154170
private byte[] payload = null;
155171
private int ttl = DEFAULT_TTL;
172+
private Urgency urgency = null;
173+
private String topic = null;
156174

157175
private NotificationBuilder() {
158176
}
159177

160178
public Notification build() {
161-
return new Notification(endpoint, userPublicKey, userAuth, payload, ttl);
179+
return new Notification(endpoint, userPublicKey, userAuth, payload, ttl, urgency, topic);
162180
}
163181

164182
public NotificationBuilder endpoint(String endpoint) {
@@ -191,10 +209,25 @@ public NotificationBuilder payload(byte[] payload) {
191209
return this;
192210
}
193211

212+
public NotificationBuilder payload(String payload) {
213+
this.payload = payload.getBytes(UTF_8);
214+
return this;
215+
}
216+
194217
public NotificationBuilder ttl(int ttl) {
195218
this.ttl = ttl;
196219
return this;
197220
}
221+
222+
public NotificationBuilder urgency(Urgency urgency) {
223+
this.urgency = urgency;
224+
return this;
225+
}
226+
227+
public NotificationBuilder topic(String topic) {
228+
this.topic = topic;
229+
return this;
230+
}
198231
}
199232

200233
}

src/main/java/nl/martijndwars/webpush/PushService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ public HttpPost preparePost(Notification notification, Encoding encoding) throws
203203
httpPost.addHeader("Urgency", notification.getUrgency().getHeaderValue());
204204
}
205205

206+
if (notification.hasTopic()) {
207+
httpPost.addHeader("Topic", notification.getTopic());
208+
}
209+
206210
Map<String, String> headers = new HashMap<>();
207211

208212
if (notification.hasPayload()) {

0 commit comments

Comments
 (0)