2016-03-24 16:46:37 +00:00
|
|
|
package main
|
|
|
|
|
2016-03-24 16:53:45 +00:00
|
|
|
import (
|
2016-03-24 16:46:37 +00:00
|
|
|
apns "github.com/sideshow/apns2"
|
|
|
|
"github.com/sideshow/apns2/certificate"
|
2016-03-24 16:53:45 +00:00
|
|
|
"github.com/sideshow/apns2/payload"
|
2016-03-25 01:23:46 +00:00
|
|
|
_ "github.com/google/go-gcm"
|
2016-03-24 16:46:37 +00:00
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ExtendJSON struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Value string `json:"val"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RequestPushNotification struct {
|
|
|
|
// Common
|
|
|
|
Tokens []string `json:"tokens" binding:"required"`
|
|
|
|
Platform int `json:"platform" binding:"required"`
|
|
|
|
Message string `json:"message" binding:"required"`
|
2016-03-24 16:53:45 +00:00
|
|
|
Priority string `json:"priority,omitempty"`
|
2016-03-25 01:34:22 +00:00
|
|
|
ContentAvailable bool `json:"content_available,omitempty"`
|
|
|
|
|
2016-03-24 16:46:37 +00:00
|
|
|
// Android
|
|
|
|
CollapseKey string `json:"collapse_key,omitempty"`
|
|
|
|
DelayWhileIdle bool `json:"delay_while_idle,omitempty"`
|
|
|
|
TimeToLive int `json:"time_to_live,omitempty"`
|
2016-03-25 01:34:22 +00:00
|
|
|
|
2016-03-24 16:46:37 +00:00
|
|
|
// iOS
|
2016-03-24 16:53:45 +00:00
|
|
|
ApnsID string `json:"apns_id,omitempty"`
|
|
|
|
Topic string `json:"topic,omitempty"`
|
2016-03-24 16:46:37 +00:00
|
|
|
Badge int `json:"badge,omitempty"`
|
|
|
|
Sound string `json:"sound,omitempty"`
|
|
|
|
Expiry int `json:"expiry,omitempty"`
|
|
|
|
Retry int `json:"retry,omitempty"`
|
|
|
|
Extend []ExtendJSON `json:"extend,omitempty"`
|
|
|
|
// meta
|
|
|
|
IDs []uint64 `json:"seq_id,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func pushNotification(notification RequestPushNotification) bool {
|
|
|
|
var (
|
2016-03-24 16:53:45 +00:00
|
|
|
success bool
|
2016-03-24 16:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
cert, err := certificate.FromPemFile("./key.pem", "")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Cert Error:", err)
|
|
|
|
}
|
|
|
|
|
2016-03-24 16:53:45 +00:00
|
|
|
apnsClient := apns.NewClient(cert).Development()
|
2016-03-24 16:46:37 +00:00
|
|
|
|
|
|
|
switch notification.Platform {
|
|
|
|
case PlatFormIos:
|
|
|
|
success = pushNotificationIos(notification, apnsClient)
|
|
|
|
if !success {
|
|
|
|
apnsClient = nil
|
|
|
|
}
|
|
|
|
case PlatFormAndroid:
|
|
|
|
success = pushNotificationAndroid(notification)
|
|
|
|
}
|
|
|
|
|
|
|
|
return success
|
|
|
|
}
|
|
|
|
|
|
|
|
func pushNotificationIos(req RequestPushNotification, client *apns.Client) bool {
|
|
|
|
|
|
|
|
for _, token := range req.Tokens {
|
|
|
|
notification := &apns.Notification{}
|
|
|
|
notification.DeviceToken = token
|
|
|
|
|
|
|
|
if len(req.ApnsID) > 0 {
|
|
|
|
notification.ApnsID = req.ApnsID
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Topic) > 0 {
|
|
|
|
notification.Topic = req.Topic
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Priority) > 0 && req.Priority == "low" {
|
|
|
|
notification.Priority = apns.PriorityLow
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := payload.NewPayload().Alert(req.Message)
|
|
|
|
|
|
|
|
if req.Badge > 0 {
|
|
|
|
payload.Badge(req.Badge)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Sound) > 0 {
|
|
|
|
payload.Sound(req.Sound)
|
|
|
|
}
|
|
|
|
|
2016-03-25 01:34:22 +00:00
|
|
|
if req.ContentAvailable {
|
|
|
|
payload.ContentAvailable()
|
|
|
|
}
|
|
|
|
|
2016-03-24 16:46:37 +00:00
|
|
|
if len(req.Extend) > 0 {
|
|
|
|
for _, extend := range req.Extend {
|
|
|
|
payload.Custom(extend.Key, extend.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notification.Payload = payload
|
|
|
|
|
|
|
|
// send ios notification
|
|
|
|
res, err := client.Push(notification)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-03-24 16:53:45 +00:00
|
|
|
log.Println("There was an error", err)
|
|
|
|
return false
|
2016-03-24 16:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.Sent() {
|
2016-03-24 16:53:45 +00:00
|
|
|
log.Println("APNs ID:", res.ApnsID)
|
2016-03-24 16:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client = nil
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func pushNotificationAndroid(req RequestPushNotification) bool {
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|