Add global ios client.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
1dfa1a7fc5
commit
86654170c0
|
@ -2,9 +2,11 @@ package main
|
|||
|
||||
import (
|
||||
"crypto/tls"
|
||||
apns "github.com/sideshow/apns2"
|
||||
)
|
||||
|
||||
var (
|
||||
PushConf ConfYaml
|
||||
CertificatePemIos tls.Certificate
|
||||
ApnsClient *apns.Client
|
||||
)
|
||||
|
|
18
main.go
18
main.go
|
@ -6,6 +6,7 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
apns "github.com/sideshow/apns2"
|
||||
"github.com/sideshow/apns2/certificate"
|
||||
)
|
||||
|
||||
|
@ -70,13 +71,22 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemKeyPath, "")
|
||||
if PushConf.Ios.Enabled {
|
||||
CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.PemKeyPath, "")
|
||||
|
||||
if err != nil {
|
||||
log.Println("Cert Error:", err)
|
||||
if err != nil {
|
||||
log.Println("Cert Error:", err)
|
||||
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
if PushConf.Ios.Production {
|
||||
ApnsClient = apns.NewClient(CertificatePemIos).Production()
|
||||
} else {
|
||||
ApnsClient = apns.NewClient(CertificatePemIos).Development()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
endless.ListenAndServe(":"+PushConf.Core.Port, GetMainEngine())
|
||||
}
|
||||
|
|
|
@ -61,21 +61,11 @@ type RequestPushNotification struct {
|
|||
func pushNotification(notification RequestPushNotification) bool {
|
||||
var (
|
||||
success bool
|
||||
apnsClient *apns.Client
|
||||
)
|
||||
|
||||
if PushConf.Ios.Production {
|
||||
apnsClient = apns.NewClient(CertificatePemIos).Production()
|
||||
} else {
|
||||
apnsClient = apns.NewClient(CertificatePemIos).Development()
|
||||
}
|
||||
|
||||
switch notification.Platform {
|
||||
case PlatFormIos:
|
||||
success = pushNotificationIos(notification, apnsClient)
|
||||
if !success {
|
||||
apnsClient = nil
|
||||
}
|
||||
success = pushNotificationIos(notification)
|
||||
case PlatFormAndroid:
|
||||
success = pushNotificationAndroid(notification)
|
||||
}
|
||||
|
@ -83,7 +73,7 @@ func pushNotification(notification RequestPushNotification) bool {
|
|||
return success
|
||||
}
|
||||
|
||||
func pushNotificationIos(req RequestPushNotification, client *apns.Client) bool {
|
||||
func pushNotificationIos(req RequestPushNotification) bool {
|
||||
|
||||
for _, token := range req.Tokens {
|
||||
notification := &apns.Notification{}
|
||||
|
@ -168,7 +158,7 @@ func pushNotificationIos(req RequestPushNotification, client *apns.Client) bool
|
|||
notification.Payload = payload
|
||||
|
||||
// send ios notification
|
||||
res, err := client.Push(notification)
|
||||
res, err := ApnsClient.Push(notification)
|
||||
|
||||
if err != nil {
|
||||
log.Println("There was an error", err)
|
||||
|
@ -177,11 +167,10 @@ func pushNotificationIos(req RequestPushNotification, client *apns.Client) bool
|
|||
|
||||
if res.Sent() {
|
||||
log.Println("APNs ID:", res.ApnsID)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
client = nil
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue