forked from SherClockHolmes/webpush-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
41 lines (36 loc) · 756 Bytes
/
Copy pathmain.go
File metadata and controls
41 lines (36 loc) · 756 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"context"
"encoding/json"
"time"
webpush "github.com/ergochat/webpush-go/v2"
)
const (
subscription = ``
vapidPrivateKey = ""
)
func main() {
// Decode subscription
sub := &webpush.Subscription{}
json.Unmarshal([]byte(subscription), sub)
// Decode VAPID keys
v := &webpush.VAPIDKeys{}
json.Unmarshal([]byte(vapidPrivateKey), v)
// Send Notification
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := webpush.SendNotification(
ctx,
[]byte("Test"),
sub,
&webpush.Options{
Subscriber: "example@example.com", // Do not include "mailto:"
VAPIDKeys: v,
TTL: 30,
},
)
if err != nil {
// TODO: Handle error
}
defer resp.Body.Close()
}