-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
56 lines (45 loc) · 1.2 KB
/
function.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
package memoirnotification
import (
"encoding/json"
"net/http"
expo "github.com/oliveroneill/exponent-server-sdk-golang/sdk"
)
type NotificationRequest struct {
Token []string `json:"token"`
Title string `json:"title"`
Body string `json:"body"`
URLScheme string `json:"urlScheme"`
}
func SendNotification(w http.ResponseWriter, r *http.Request) {
param := NotificationRequest{}
if err := json.NewDecoder(r.Body).Decode(¶m); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
to := []expo.ExponentPushToken{}
for _, token := range param.Token {
pushToken, err := expo.NewExponentPushToken(token)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
to = append(to, pushToken)
}
client := expo.NewPushClient(nil)
_, err := client.Publish(
&expo.PushMessage{
To: to,
Body: param.Body,
Data: map[string]string{"urlScheme": param.URLScheme},
Sound: "default",
Title: param.Title,
Priority: expo.DefaultPriority,
},
)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}