diff --git a/gorush.go b/gorush.go index fe135b6..7e48168 100644 --- a/gorush.go +++ b/gorush.go @@ -72,9 +72,6 @@ func main() { // send android notification if *android { - - checkInput(*token, *message) - gorush.PushConf.Android.Enabled = true req := gorush.PushNotification{ Tokens: []string{*token}, @@ -82,7 +79,7 @@ func main() { Message: *message, } - err := gorush.CheckGCMMessage(req) + err := gorush.CheckMessage(req) if err != nil { gorush.LogError.Fatal(err) @@ -96,8 +93,6 @@ func main() { // send android notification if *ios { - checkInput(*token, *message) - if *production { gorush.PushConf.Ios.Production = true } @@ -109,6 +104,12 @@ func main() { Message: *message, } + err := gorush.CheckMessage(req) + + if err != nil { + gorush.LogError.Fatal(err) + } + gorush.InitAppStatus() gorush.InitAPNSClient() gorush.PushToIOS(req) diff --git a/gorush/notification.go b/gorush/notification.go index 271d740..2dc8949 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -78,7 +78,7 @@ type PushNotification struct { } // CheckGCMMessage for check GCM Message -func CheckGCMMessage(req PushNotification) error { +func CheckMessage(req PushNotification) error { var msg string if req.Message == "" { msg = "the message must not be empty" @@ -92,13 +92,19 @@ func CheckGCMMessage(req PushNotification) error { return errors.New(msg) } - if len(req.Tokens) > 1000 { + if len(req.Tokens) == PlatFormIos && len(req.Tokens[0]) == 0 { + msg = "the token must not be empty" + LogAccess.Debug(msg) + return errors.New(msg) + } + + if req.Platform == PlatFormAndroid && 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 { + if req.Platform == PlatFormAndroid && (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) @@ -380,9 +386,10 @@ func PushToAndroid(req PushNotification) bool { var APIKey string // check message - err := CheckGCMMessage(req) + err := CheckMessage(req) if err != nil { + LogError.Error("request error: " + err.Error()) return false } diff --git a/gorush/notification_test.go b/gorush/notification_test.go index 5555937..d4af358 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -241,7 +241,7 @@ func TestPushToAndroidWrongAPIKey(t *testing.T) { req := PushNotification{ Tokens: []string{"aaaaaa", "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", } @@ -257,7 +257,7 @@ func TestPushToAndroidWrongToken(t *testing.T) { req := PushNotification{ Tokens: []string{"aaaaaa", "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", } @@ -277,7 +277,7 @@ func TestPushToAndroidRightTokenForJSONLog(t *testing.T) { req := PushNotification{ Tokens: []string{androidToken, "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", } @@ -295,7 +295,7 @@ func TestPushToAndroidRightTokenForStringLog(t *testing.T) { req := PushNotification{ Tokens: []string{androidToken, "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", } @@ -313,7 +313,7 @@ func TestOverwriteAndroidAPIKey(t *testing.T) { req := PushNotification{ Tokens: []string{androidToken, "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", // overwrite android api key APIKey: "1234", @@ -342,13 +342,13 @@ func TestSenMultipleNotifications(t *testing.T) { //ios { Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, - Platform: 1, + Platform: PlatFormIos, Message: "Welcome", }, // android { Tokens: []string{androidToken, "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", }, }, @@ -375,13 +375,13 @@ func TestDisabledAndroidNotifications(t *testing.T) { //ios { Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, - Platform: 1, + Platform: PlatFormIos, Message: "Welcome", }, // android { Tokens: []string{androidToken, "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", }, }, @@ -408,13 +408,13 @@ func TestDisabledIosNotifications(t *testing.T) { //ios { Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, - Platform: 1, + Platform: PlatFormIos, Message: "Welcome", }, // android { Tokens: []string{androidToken, "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", }, }, @@ -464,7 +464,7 @@ func TestGCMMessage(t *testing.T) { Message: "", } - err = CheckGCMMessage(req) + err = CheckMessage(req) assert.Error(t, err) // the message must specify at least one registration ID @@ -473,37 +473,49 @@ func TestGCMMessage(t *testing.T) { Tokens: []string{}, } - err = CheckGCMMessage(req) + err = CheckMessage(req) + assert.Error(t, err) + + // the token must not be empty + req = PushNotification{ + Message: "Test", + Tokens: []string{""}, + } + + err = CheckMessage(req) assert.Error(t, err) // the message may specify at most 1000 registration IDs req = PushNotification{ Message: "Test", + Platform: PlatFormAndroid, Tokens: make([]string, 1001), } - err = CheckGCMMessage(req) + err = CheckMessage(req) assert.Error(t, err) // the message's TimeToLive field must be an integer // between 0 and 2419200 (4 weeks) req = PushNotification{ Message: "Test", + Platform: PlatFormAndroid, Tokens: []string{"XXXXXXXXX"}, TimeToLive: 2419201, } - err = CheckGCMMessage(req) + err = CheckMessage(req) assert.Error(t, err) // Pass req = PushNotification{ Message: "Test", + Platform: PlatFormAndroid, Tokens: []string{"XXXXXXXXX"}, TimeToLive: 86400, } - err = CheckGCMMessage(req) + err = CheckMessage(req) assert.NoError(t, err) } @@ -515,7 +527,7 @@ func TestCheckAndroidMessage(t *testing.T) { req := PushNotification{ Tokens: []string{"aaaaaa", "bbbbb"}, - Platform: 2, + Platform: PlatFormAndroid, Message: "Welcome", TimeToLive: 2419201, } diff --git a/gorush/server_test.go b/gorush/server_test.go index c0946b4..68b578b 100644 --- a/gorush/server_test.go +++ b/gorush/server_test.go @@ -156,12 +156,12 @@ func TestOutOfRangeMaxNotifications(t *testing.T) { "notifications": []gofight.D{ { "tokens": []string{"aaaaa", "bbbbb"}, - "platform": 2, + "platform": PlatFormAndroid, "message": "Welcome", }, { "tokens": []string{"aaaaa", "bbbbb"}, - "platform": 2, + "platform": PlatFormAndroid, "message": "Welcome", }, }, @@ -187,7 +187,7 @@ func TestSuccessPushHandler(t *testing.T) { "notifications": []gofight.D{ { "tokens": []string{androidToken, "bbbbb"}, - "platform": 2, + "platform": PlatFormAndroid, "message": "Welcome", }, },