Add global ios client.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-03-26 23:42:22 +08:00
parent 1dfa1a7fc5
commit 86654170c0
3 changed files with 20 additions and 19 deletions

View File

@ -2,9 +2,11 @@ package main
import ( import (
"crypto/tls" "crypto/tls"
apns "github.com/sideshow/apns2"
) )
var ( var (
PushConf ConfYaml PushConf ConfYaml
CertificatePemIos tls.Certificate CertificatePemIos tls.Certificate
ApnsClient *apns.Client
) )

10
main.go
View File

@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"log" "log"
"net/http" "net/http"
apns "github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate" "github.com/sideshow/apns2/certificate"
) )
@ -70,6 +71,7 @@ func main() {
return return
} }
if PushConf.Ios.Enabled {
CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemKeyPath, "") CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemKeyPath, "")
if err != nil { if err != nil {
@ -78,5 +80,13 @@ func main() {
return return
} }
if PushConf.Ios.Production {
ApnsClient = apns.NewClient(CertificatePemIos).Production()
} else {
ApnsClient = apns.NewClient(CertificatePemIos).Development()
}
}
endless.ListenAndServe(":"+PushConf.Core.Port, GetMainEngine()) endless.ListenAndServe(":"+PushConf.Core.Port, GetMainEngine())
} }

View File

@ -61,21 +61,11 @@ type RequestPushNotification struct {
func pushNotification(notification RequestPushNotification) bool { func pushNotification(notification RequestPushNotification) bool {
var ( var (
success bool success bool
apnsClient *apns.Client
) )
if PushConf.Ios.Production {
apnsClient = apns.NewClient(CertificatePemIos).Production()
} else {
apnsClient = apns.NewClient(CertificatePemIos).Development()
}
switch notification.Platform { switch notification.Platform {
case PlatFormIos: case PlatFormIos:
success = pushNotificationIos(notification, apnsClient) success = pushNotificationIos(notification)
if !success {
apnsClient = nil
}
case PlatFormAndroid: case PlatFormAndroid:
success = pushNotificationAndroid(notification) success = pushNotificationAndroid(notification)
} }
@ -83,7 +73,7 @@ func pushNotification(notification RequestPushNotification) bool {
return success return success
} }
func pushNotificationIos(req RequestPushNotification, client *apns.Client) bool { func pushNotificationIos(req RequestPushNotification) bool {
for _, token := range req.Tokens { for _, token := range req.Tokens {
notification := &apns.Notification{} notification := &apns.Notification{}
@ -168,7 +158,7 @@ func pushNotificationIos(req RequestPushNotification, client *apns.Client) bool
notification.Payload = payload notification.Payload = payload
// send ios notification // send ios notification
res, err := client.Push(notification) res, err := ApnsClient.Push(notification)
if err != nil { if err != nil {
log.Println("There was an error", err) log.Println("There was an error", err)
@ -177,11 +167,10 @@ func pushNotificationIos(req RequestPushNotification, client *apns.Client) bool
if res.Sent() { if res.Sent() {
log.Println("APNs ID:", res.ApnsID) log.Println("APNs ID:", res.ApnsID)
return true
} }
} }
client = nil
return true return true
} }