commit
635c02e810
25
gorush.go
25
gorush.go
|
@ -6,6 +6,16 @@ import (
|
|||
"log"
|
||||
)
|
||||
|
||||
func checkInput(token, message string) {
|
||||
if len(token) == 0 {
|
||||
gorush.LogError.Fatal("Missing token flag (-t)")
|
||||
}
|
||||
|
||||
if len(message) == 0 {
|
||||
gorush.LogError.Fatal("Missing message flag (-m)")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
version := flag.Bool("v", false, "gorush version")
|
||||
confPath := flag.String("c", "", "yaml configuration file path for gorush")
|
||||
|
@ -62,13 +72,8 @@ func main() {
|
|||
|
||||
// send android notification
|
||||
if *android {
|
||||
if len(*token) == 0 {
|
||||
gorush.LogError.Fatal("Missing token flag (-t)")
|
||||
}
|
||||
|
||||
if len(*message) == 0 {
|
||||
gorush.LogError.Fatal("Missing message flag (-m)")
|
||||
}
|
||||
checkInput(*token, *message)
|
||||
|
||||
gorush.PushConf.Android.Enabled = true
|
||||
req := gorush.PushNotification{
|
||||
|
@ -85,13 +90,7 @@ func main() {
|
|||
|
||||
// send android notification
|
||||
if *ios {
|
||||
if len(*token) == 0 {
|
||||
gorush.LogError.Fatal("Missing token flag (-t)")
|
||||
}
|
||||
|
||||
if len(*message) == 0 {
|
||||
gorush.LogError.Fatal("Missing message flag (-m)")
|
||||
}
|
||||
checkInput(*token, *message)
|
||||
|
||||
if *production {
|
||||
gorush.PushConf.Ios.Production = true
|
||||
|
|
|
@ -166,48 +166,7 @@ func queueNotification(req RequestPush) int {
|
|||
return count
|
||||
}
|
||||
|
||||
// GetIOSNotification use for define iOS notificaiton.
|
||||
// The iOS Notification Payload
|
||||
// ref: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html
|
||||
func GetIOSNotification(req PushNotification) *apns.Notification {
|
||||
notification := &apns.Notification{}
|
||||
|
||||
if len(req.ApnsID) > 0 {
|
||||
notification.ApnsID = req.ApnsID
|
||||
}
|
||||
|
||||
if len(req.Topic) > 0 {
|
||||
notification.Topic = req.Topic
|
||||
}
|
||||
|
||||
if req.Expiration > 0 {
|
||||
notification.Expiration = time.Unix(req.Expiration, 0)
|
||||
}
|
||||
|
||||
if len(req.Priority) > 0 && req.Priority == "normal" {
|
||||
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)
|
||||
}
|
||||
|
||||
if req.ContentAvailable {
|
||||
payload.ContentAvailable()
|
||||
}
|
||||
|
||||
if len(req.Data) > 0 {
|
||||
for k, v := range req.Data {
|
||||
payload.Custom(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
func iosAlertDictionary(payload *payload.Payload, req PushNotification) *payload.Payload {
|
||||
// Alert dictionary
|
||||
|
||||
if len(req.Title) > 0 {
|
||||
|
@ -252,10 +211,50 @@ func GetIOSNotification(req PushNotification) *apns.Notification {
|
|||
payload.Category(req.Category)
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
// GetIOSNotification use for define iOS notificaiton.
|
||||
// The iOS Notification Payload
|
||||
// ref: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html
|
||||
func GetIOSNotification(req PushNotification) *apns.Notification {
|
||||
notification := &apns.Notification{
|
||||
ApnsID: req.ApnsID,
|
||||
Topic: req.Topic,
|
||||
}
|
||||
|
||||
if req.Expiration > 0 {
|
||||
notification.Expiration = time.Unix(req.Expiration, 0)
|
||||
}
|
||||
|
||||
if len(req.Priority) > 0 && req.Priority == "normal" {
|
||||
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)
|
||||
}
|
||||
|
||||
if req.ContentAvailable {
|
||||
payload.ContentAvailable()
|
||||
}
|
||||
|
||||
if len(req.URLArgs) > 0 {
|
||||
payload.URLArgs(req.URLArgs)
|
||||
}
|
||||
|
||||
for k, v := range req.Data {
|
||||
payload.Custom(k, v)
|
||||
}
|
||||
|
||||
payload = iosAlertDictionary(payload, req)
|
||||
|
||||
notification.Payload = payload
|
||||
|
||||
return notification
|
||||
|
@ -303,42 +302,22 @@ func PushToIOS(req PushNotification) bool {
|
|||
// HTTP Connection Server Reference for Android
|
||||
// https://developers.google.com/cloud-messaging/http-server-ref
|
||||
func GetAndroidNotification(req PushNotification) gcm.HttpMessage {
|
||||
notification := gcm.HttpMessage{}
|
||||
notification := gcm.HttpMessage{
|
||||
To: req.To,
|
||||
CollapseKey: req.CollapseKey,
|
||||
ContentAvailable: req.ContentAvailable,
|
||||
DelayWhileIdle: req.DelayWhileIdle,
|
||||
TimeToLive: req.TimeToLive,
|
||||
RestrictedPackageName: req.RestrictedPackageName,
|
||||
DryRun: req.DryRun,
|
||||
}
|
||||
|
||||
notification.RegistrationIds = req.Tokens
|
||||
|
||||
if len(req.To) > 0 {
|
||||
notification.To = req.To
|
||||
}
|
||||
|
||||
if len(req.Priority) > 0 && req.Priority == "high" {
|
||||
notification.Priority = "high"
|
||||
}
|
||||
|
||||
if len(req.CollapseKey) > 0 {
|
||||
notification.CollapseKey = req.CollapseKey
|
||||
}
|
||||
|
||||
if req.ContentAvailable {
|
||||
notification.ContentAvailable = true
|
||||
}
|
||||
|
||||
if req.DelayWhileIdle {
|
||||
notification.DelayWhileIdle = true
|
||||
}
|
||||
|
||||
if req.TimeToLive > 0 {
|
||||
notification.TimeToLive = req.TimeToLive
|
||||
}
|
||||
|
||||
if len(req.RestrictedPackageName) > 0 {
|
||||
notification.RestrictedPackageName = req.RestrictedPackageName
|
||||
}
|
||||
|
||||
if req.DryRun {
|
||||
notification.DryRun = true
|
||||
}
|
||||
|
||||
// Add another field
|
||||
if len(req.Data) > 0 {
|
||||
notification.Data = make(map[string]interface{})
|
||||
|
|
Loading…
Reference in New Issue