refactor: Support empty notification for android (#170)

This commit is contained in:
Bo-Yi Wu 2017-01-21 12:18:41 +08:00 committed by GitHub
parent da1231315f
commit 9bd6886c1e
2 changed files with 11 additions and 14 deletions

View File

@ -86,11 +86,6 @@ type PushNotification struct {
// CheckMessage for check request message // CheckMessage for check request message
func CheckMessage(req PushNotification) error { func CheckMessage(req PushNotification) error {
var msg string 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 { if len(req.Tokens) == 0 {
msg = "the message must specify at least one registration ID" msg = "the message must specify at least one registration ID"
@ -432,7 +427,7 @@ func GetAndroidNotification(req PushNotification) gcm.HttpMessage {
notification.Notification = &req.Notification notification.Notification = &req.Notification
// Set request message if body is empty // Set request message if body is empty
if len(notification.Notification.Body) == 0 { if len(req.Message) > 0 {
notification.Notification.Body = req.Message notification.Notification.Body = req.Message
} }

View File

@ -407,6 +407,16 @@ func TestAndroidNotificationStructure(t *testing.T) {
assert.Equal(t, "Welcome", notification.Notification.Body) assert.Equal(t, "Welcome", notification.Notification.Body)
assert.Equal(t, "1", notification.Data["a"]) assert.Equal(t, "1", notification.Data["a"])
assert.Equal(t, 2, notification.Data["b"]) assert.Equal(t, 2, notification.Data["b"])
// test empty body
req = PushNotification{
Tokens: []string{"a", "b"},
To: test,
}
notification = GetAndroidNotification(req)
assert.Equal(t, test, notification.To)
assert.Equal(t, "", notification.Notification.Body)
} }
func TestPushToIOS(t *testing.T) { func TestPushToIOS(t *testing.T) {
@ -654,14 +664,6 @@ func TestGCMMessage(t *testing.T) {
var req PushNotification var req PushNotification
var err error 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 // the message must specify at least one registration ID
req = PushNotification{ req = PushNotification{
Message: "Test", Message: "Test",