From b92103068892cda2e94255bceaecf6f71857c4af Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 24 Apr 2016 14:30:17 +0800 Subject: [PATCH] check android message. Signed-off-by: Bo-Yi Wu --- gorush.go | 6 ++++ gorush/notification.go | 38 ++++++++++++++++++++ gorush/notification_test.go | 69 +++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) diff --git a/gorush.go b/gorush.go index 93dfe6d..fe135b6 100644 --- a/gorush.go +++ b/gorush.go @@ -82,6 +82,12 @@ func main() { Message: *message, } + err := gorush.CheckGCMMessage(req) + + if err != nil { + gorush.LogError.Fatal(err) + } + gorush.InitAppStatus() gorush.PushToAndroid(req) diff --git a/gorush/notification.go b/gorush/notification.go index 182bb65..271d740 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -77,6 +77,37 @@ type PushNotification struct { Alert Alert `json:"alert,omitempty"` } +// CheckGCMMessage for check GCM Message +func CheckGCMMessage(req PushNotification) error { + var msg string + if req.Message == "" { + msg = "the message must not be empty" + LogAccess.Debug(msg) + return errors.New(msg) + } + + if len(req.Tokens) == 0 { + msg = "the message must specify at least one registration ID" + LogAccess.Debug(msg) + return errors.New(msg) + } + + if len(req.Tokens) > 1000 { + msg = "the message may specify at most 1000 registration IDs" + LogAccess.Debug(msg) + return errors.New(msg) + } + + if req.TimeToLive < 0 || 2419200 < req.TimeToLive { + msg = "the message's TimeToLive field must be an integer " + + "between 0 and 2419200 (4 weeks)" + LogAccess.Debug(msg) + return errors.New(msg) + } + + return nil +} + // CheckPushConf provide check your yml config. func CheckPushConf() error { if !PushConf.Ios.Enabled && !PushConf.Android.Enabled { @@ -348,6 +379,13 @@ func GetAndroidNotification(req PushNotification) gcm.HttpMessage { func PushToAndroid(req PushNotification) bool { var APIKey string + // check message + err := CheckGCMMessage(req) + + if err != nil { + return false + } + notification := GetAndroidNotification(req) if APIKey = PushConf.Android.APIKey; req.APIKey != "" { diff --git a/gorush/notification_test.go b/gorush/notification_test.go index 746587c..5555937 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -454,3 +454,72 @@ func TestAPNSClientProdHost(t *testing.T) { assert.Equal(t, apns2.HostProduction, ApnsClient.Host) } + +func TestGCMMessage(t *testing.T) { + var req PushNotification + var err error + + // the message must not be empty + req = PushNotification{ + Message: "", + } + + err = CheckGCMMessage(req) + assert.Error(t, err) + + // the message must specify at least one registration ID + req = PushNotification{ + Message: "Test", + Tokens: []string{}, + } + + err = CheckGCMMessage(req) + assert.Error(t, err) + + // the message may specify at most 1000 registration IDs + req = PushNotification{ + Message: "Test", + Tokens: make([]string, 1001), + } + + err = CheckGCMMessage(req) + assert.Error(t, err) + + // the message's TimeToLive field must be an integer + // between 0 and 2419200 (4 weeks) + req = PushNotification{ + Message: "Test", + Tokens: []string{"XXXXXXXXX"}, + TimeToLive: 2419201, + } + + err = CheckGCMMessage(req) + assert.Error(t, err) + + // Pass + req = PushNotification{ + Message: "Test", + Tokens: []string{"XXXXXXXXX"}, + TimeToLive: 86400, + } + + err = CheckGCMMessage(req) + assert.NoError(t, err) +} + +func TestCheckAndroidMessage(t *testing.T) { + PushConf = BuildDefaultPushConf() + + PushConf.Android.Enabled = true + PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") + + req := PushNotification{ + Tokens: []string{"aaaaaa", "bbbbb"}, + Platform: 2, + Message: "Welcome", + TimeToLive: 2419201, + } + + success := PushToAndroid(req) + assert.False(t, success) +}