Skip to content

Commit

Permalink
refactor: Support empty notification for android (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy authored Jan 21, 2017
1 parent da12313 commit 9bd6886
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
7 changes: 1 addition & 6 deletions gorush/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ type PushNotification struct {
// 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"
Expand Down Expand Up @@ -432,7 +427,7 @@ func GetAndroidNotification(req PushNotification) gcm.HttpMessage {
notification.Notification = &req.Notification

// Set request message if body is empty
if len(notification.Notification.Body) == 0 {
if len(req.Message) > 0 {
notification.Notification.Body = req.Message
}

Expand Down
18 changes: 10 additions & 8 deletions gorush/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,16 @@ func TestAndroidNotificationStructure(t *testing.T) {
assert.Equal(t, "Welcome", notification.Notification.Body)
assert.Equal(t, "1", notification.Data["a"])
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) {
Expand Down Expand Up @@ -654,14 +664,6 @@ 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",
Expand Down

0 comments on commit 9bd6886

Please sign in to comment.