Skip to content

Commit c4fc985

Browse files
authored
refactor(fcm): test Android notification handling (#786)
- Refactor JSON marshaling logic in `GetAndroidNotification` function - Add test case for JSON data structure in `TestAndroidNotificationStructure` function Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent b3b7733 commit c4fc985

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

notify/notification_fcm.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ func GetAndroidNotification(req *PushNotification) []*messaging.Message {
9696
case string:
9797
data[k] = fmt.Sprintf("%s", v)
9898
default:
99-
jsonValue, err := json.Marshal(v)
100-
if err == nil {
101-
data[k] = string(jsonValue)
99+
if v, err := json.Marshal(v); err == nil {
100+
data[k] = string(v)
102101
}
103102
}
104103
}

notify/notification_fcm_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ func TestAndroidNotificationStructure(t *testing.T) {
159159
Data: D{
160160
"a": "1",
161161
"b": 2,
162+
"json": map[string]interface{}{
163+
"c": "3",
164+
"d": 4,
165+
},
162166
},
163167
Notification: &messaging.Notification{
164168
Title: test,
@@ -172,6 +176,7 @@ func TestAndroidNotificationStructure(t *testing.T) {
172176
assert.Equal(t, "Welcome", messages[0].Notification.Body)
173177
assert.Equal(t, "1", messages[0].Data["a"])
174178
assert.Equal(t, "2", messages[0].Data["b"])
179+
assert.Equal(t, "{\"c\":\"3\",\"d\":4}", messages[0].Data["json"])
175180

176181
// test empty body
177182
req = &PushNotification{

0 commit comments

Comments
 (0)