From 9bd6886c1ebcc4ee174f1bb7c724d0e040d1be82 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 21 Jan 2017 12:18:41 +0800 Subject: [PATCH] refactor: Support empty notification for android (#170) --- gorush/notification.go | 7 +------ gorush/notification_test.go | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/gorush/notification.go b/gorush/notification.go index 7ca9fba..b595cbf 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -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" @@ -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 } diff --git a/gorush/notification_test.go b/gorush/notification_test.go index f83f9c4..9ea2d82 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -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) { @@ -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",