Skip to content

Commit 84eef06

Browse files
chapsukSherClockHolmes
authored andcommitted
Add urgency and topic optional headers (SherClockHolmes#8)
* Add urgency and topic optional headers * Added urgency type and urgency constants
1 parent 739e7e5 commit 84eef06

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

webpush.go

+35
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ import (
2020
"golang.org/x/crypto/hkdf"
2121
)
2222

23+
// Urgency indicates to the push service how important a message is to the user.
24+
// This can be used by the push service to help conserve the battery life of a user's device
25+
// by only waking up for important messages when battery is low.
26+
type Urgency string
27+
28+
const (
29+
// UrgencyVeryLow requires device state: on power and Wi-Fi
30+
UrgencyVeryLow Urgency = "very-low"
31+
// UrgencyLow requires device state: on either power or Wi-Fi
32+
UrgencyLow Urgency = "low"
33+
// UrgencyNormal excludes device state: low battery
34+
UrgencyNormal Urgency = "normal"
35+
// UrgencyHigh admits device state: low battery
36+
UrgencyHigh Urgency = "high"
37+
)
38+
2339
var saltFunc = func() ([]byte, error) {
2440
salt := make([]byte, 16)
2541
_, err := io.ReadFull(rand.Reader, salt)
@@ -39,7 +55,9 @@ type HTTPClient interface {
3955
type Options struct {
4056
HTTPClient HTTPClient // Will replace with *http.Client by default if not included
4157
Subscriber string // Sub in VAPID JWT token
58+
Topic string // Set the Topic header to collapse a pending messages (Optional)
4259
TTL int // Set the TTL on the endpoint POST request
60+
Urgency Urgency // Set the Urgency header to change a message priority (Optional)
4361
VAPIDPrivateKey string // Used to sign VAPID JWT token
4462
}
4563

@@ -151,6 +169,14 @@ func SendNotification(message []byte, s *Subscription, options *Options) (*http.
151169
req.Header.Set("Content-Encoding", "aesgcm")
152170
req.Header.Set("TTL", strconv.Itoa(options.TTL))
153171

172+
// Сhecking the optional headers
173+
if isValidUrgency(options.Urgency) {
174+
req.Header.Set("Urgency", string(options.Urgency))
175+
}
176+
if len(options.Topic) > 0 {
177+
req.Header.Set("Topic", options.Topic)
178+
}
179+
154180
// Set VAPID headers
155181
err = vapid(req, s, options)
156182
if err != nil {
@@ -223,3 +249,12 @@ func getInfo(infoType, clientPublicKey, serverPublicKey []byte) []byte {
223249

224250
return info.Bytes()
225251
}
252+
253+
// Checking allowable values for the urgency header
254+
func isValidUrgency(urgency Urgency) bool {
255+
switch urgency {
256+
case UrgencyVeryLow, UrgencyLow, UrgencyNormal, UrgencyHigh:
257+
return true
258+
}
259+
return false
260+
}

webpush_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ func TestSendNotification(t *testing.T) {
2929
resp, err := SendNotification([]byte("Test"), getTestSubscription(), &Options{
3030
HTTPClient: &testHTTPClient{},
3131
Subscriber: "mailto:<EMAIL@EXAMPLE.COM>",
32+
Topic: "test_topic",
3233
TTL: 0,
34+
Urgency: "low",
3335
VAPIDPrivateKey: "testKey",
3436
})
3537

0 commit comments

Comments
 (0)