-
Notifications
You must be signed in to change notification settings - Fork 244
/
notifier.go
100 lines (83 loc) · 1.89 KB
/
notifier.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package notifier
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
)
const (
// IOS identifier for an iOS notification.
IOS = 1
// Android identifier for an android notification.
Android = 2
failedPushErrorType = "failed-push"
pushEndpoint = "/api/push"
)
// Notifier handles android and ios push notifications.
type Notifier struct {
client *http.Client
url string
}
// New notifier connected to the specified server.
func New(url string) *Notifier {
client := &http.Client{}
return &Notifier{url: url, client: client}
}
// Notification details for gorush.
type Notification struct {
Tokens []string `json:"tokens"`
Platform float32 `json:"platform"`
Message string `json:"message"`
}
type request struct {
Notifications []*Notification `json:"notifications"`
}
// Response from gorush.
type Response struct {
Logs []struct {
Type string `json:"type"`
Error string `json:"error"`
} `json:"logs"`
}
// Send a push notification to given devices.
func (n *Notifier) Send(notifications []*Notification) error {
url := n.url + pushEndpoint
r := request{Notifications: notifications}
body, err := json.Marshal(r)
if err != nil {
return err
}
res, err := n.doRequest(url, body)
if err != nil {
return err
}
if len(res.Logs) > 0 {
if res.Logs[0].Type == failedPushErrorType {
return errors.New(res.Logs[0].Error)
}
}
return err
}
func (n *Notifier) doRequest(url string, body []byte) (res Response, err error) {
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := n.client.Do(req)
if err != nil {
return
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Println(err.Error())
}
}()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return res, json.Unmarshal(body, &res)
}