Merge pull request #65 from appleboy/GCM

Fix #64 check user request message format.
This commit is contained in:
Bo-Yi Wu 2016-04-24 04:02:33 -05:00
commit 4d91f4fad2
4 changed files with 153 additions and 19 deletions

View File

@ -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,6 +79,12 @@ func main() {
Message: *message,
}
err := gorush.CheckMessage(req)
if err != nil {
gorush.LogError.Fatal(err)
}
gorush.InitAppStatus()
gorush.PushToAndroid(req)
@ -90,8 +93,6 @@ func main() {
// send android notification
if *ios {
checkInput(*token, *message)
if *production {
gorush.PushConf.Ios.Production = true
}
@ -103,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)

View File

@ -77,6 +77,44 @@ type PushNotification struct {
Alert Alert `json:"alert,omitempty"`
}
// CheckMessage for check request message
func CheckMessage(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) == 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)
}
// ref: https://developers.google.com/cloud-messaging/http-server-ref
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)
return errors.New(msg)
}
return nil
}
// CheckPushConf provide check your yml config.
func CheckPushConf() error {
if !PushConf.Ios.Enabled && !PushConf.Android.Enabled {
@ -348,6 +386,14 @@ func GetAndroidNotification(req PushNotification) gcm.HttpMessage {
func PushToAndroid(req PushNotification) bool {
var APIKey string
// check message
err := CheckMessage(req)
if err != nil {
LogError.Error("request error: " + err.Error())
return false
}
notification := GetAndroidNotification(req)
if APIKey = PushConf.Android.APIKey; req.APIKey != "" {

View File

@ -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",
},
},
@ -454,3 +454,84 @@ 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 = CheckMessage(req)
assert.Error(t, err)
// the message must specify at least one registration ID
req = PushNotification{
Message: "Test",
Tokens: []string{},
}
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 = 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 = CheckMessage(req)
assert.Error(t, err)
// Pass
req = PushNotification{
Message: "Test",
Platform: PlatFormAndroid,
Tokens: []string{"XXXXXXXXX"},
TimeToLive: 86400,
}
err = CheckMessage(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: PlatFormAndroid,
Message: "Welcome",
TimeToLive: 2419201,
}
success := PushToAndroid(req)
assert.False(t, success)
}

View File

@ -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",
},
},