@@ -20,6 +20,22 @@ import (
20
20
"golang.org/x/crypto/hkdf"
21
21
)
22
22
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
+
23
39
var saltFunc = func () ([]byte , error ) {
24
40
salt := make ([]byte , 16 )
25
41
_ , err := io .ReadFull (rand .Reader , salt )
@@ -39,7 +55,9 @@ type HTTPClient interface {
39
55
type Options struct {
40
56
HTTPClient HTTPClient // Will replace with *http.Client by default if not included
41
57
Subscriber string // Sub in VAPID JWT token
58
+ Topic string // Set the Topic header to collapse a pending messages (Optional)
42
59
TTL int // Set the TTL on the endpoint POST request
60
+ Urgency Urgency // Set the Urgency header to change a message priority (Optional)
43
61
VAPIDPrivateKey string // Used to sign VAPID JWT token
44
62
}
45
63
@@ -151,6 +169,14 @@ func SendNotification(message []byte, s *Subscription, options *Options) (*http.
151
169
req .Header .Set ("Content-Encoding" , "aesgcm" )
152
170
req .Header .Set ("TTL" , strconv .Itoa (options .TTL ))
153
171
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
+
154
180
// Set VAPID headers
155
181
err = vapid (req , s , options )
156
182
if err != nil {
@@ -223,3 +249,12 @@ func getInfo(infoType, clientPublicKey, serverPublicKey []byte) []byte {
223
249
224
250
return info .Bytes ()
225
251
}
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
+ }
0 commit comments