-
Notifications
You must be signed in to change notification settings - Fork 0
/
Apns.go
56 lines (43 loc) · 1.17 KB
/
Apns.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 toolset
import (
"encoding/json"
"fmt"
"github.com/sideshow/apns2"
"github.com/sideshow/apns2/token"
)
var (
apnsClient *apns2.Client
isInit bool
appBundleId string
)
func ApnsInit(certificate []byte, keyID string, teamID string, appBundle string) {
appBundleId = appBundle
authKey, err := token.AuthKeyFromBytes(certificate)
if err != nil {
fmt.Println(err)
}
t := &token.Token{AuthKey: authKey, KeyID: keyID, TeamID: teamID}
apnsClient = apns2.NewTokenClient(t).Production().Development()
isInit = true
}
func ApnsPush(token string, msg string) error {
if !isInit {
return fmt.Errorf("apns not init, call ApnsInit() first")
}
payload := make(map[string]interface{})
aps := make(map[string]interface{})
aps["alert"] = msg
aps["sound"] = "defalut"
aps["badge"] = 1
payload["aps"] = aps
notification := &apns2.Notification{}
notification.DeviceToken = token
notification.Topic = appBundleId
jsonPayload, _ := json.Marshal(payload)
notification.Payload = jsonPayload
res, err := apnsClient.Push(notification)
if res.StatusCode != 200 {
return fmt.Errorf("apns push error, status code: %d, %s", res.StatusCode, res.Reason)
}
return err
}