-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplepush.go
63 lines (53 loc) · 1.6 KB
/
applepush.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
package main
import (
"fmt"
"log"
"github.com/sideshow/apns2"
"github.com/sideshow/apns2/payload"
"github.com/sideshow/apns2/token"
_ "embed"
)
var applePushClient = func() *apns2.Client {
cl, err := newApplePushClient()
if err != nil {
panic(err)
}
return cl
}()
func newApplePushClient() (*apns2.Client, error) {
if len(authKey) == 0 {
return nil, nil // placeholder
}
authKey, err := token.AuthKeyFromBytes(authKey)
if err != nil {
return nil, fmt.Errorf("loading token: %v", err)
}
token := &token.Token{
AuthKey: authKey,
// KeyID from developer account (Certificates, Identifiers & Profiles -> Keys)
KeyID: apnsKeyID,
// TeamID from developer account (View Account -> Membership)
TeamID: apnsTeamID,
}
// If you want to test push notifications for builds running directly from XCode (Development), use
// client := apns2.NewClient(cert).Development()
// For apps published to the app store or installed as an ad-hoc distribution use Production()
return apns2.NewTokenClient(token).Development(), nil
}
func applePush(payload *payload.Payload) error {
if applePushClient == nil {
log.Printf("[apns] applepush_secret.go missing, ignoring notification")
return nil
}
notification := &apns2.Notification{}
notification.DeviceToken = apnsDeviceToken
notification.Topic = apnsTopic
notification.Payload = payload
log.Printf("[apns] pushing notification = %+v", notification)
res, err := applePushClient.Push(notification)
if err != nil {
return fmt.Errorf("apns: %v", err)
}
log.Printf("HTTP %d, APNS ID %q, reason %q", res.StatusCode, res.ApnsID, res.Reason)
return nil
}