check android message.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-04-24 14:30:17 +08:00
parent ba5a6799c7
commit b921030688
3 changed files with 113 additions and 0 deletions

View File

@ -82,6 +82,12 @@ func main() {
Message: *message,
}
err := gorush.CheckGCMMessage(req)
if err != nil {
gorush.LogError.Fatal(err)
}
gorush.InitAppStatus()
gorush.PushToAndroid(req)

View File

@ -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 != "" {

View File

@ -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)
}