Skip to content

Commit

Permalink
refactor(fcm): test Android notification handling (#786)
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
appleboy authored Jun 19, 2024
1 parent b3b7733 commit c4fc985
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 2 additions & 3 deletions notify/notification_fcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ func GetAndroidNotification(req *PushNotification) []*messaging.Message {
case string:
data[k] = fmt.Sprintf("%s", v)
default:
jsonValue, err := json.Marshal(v)
if err == nil {
data[k] = string(jsonValue)
if v, err := json.Marshal(v); err == nil {
data[k] = string(v)
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions notify/notification_fcm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ func TestAndroidNotificationStructure(t *testing.T) {
Data: D{
"a": "1",
"b": 2,
"json": map[string]interface{}{
"c": "3",
"d": 4,
},
},
Notification: &messaging.Notification{
Title: test,
Expand All @@ -172,6 +176,7 @@ func TestAndroidNotificationStructure(t *testing.T) {
assert.Equal(t, "Welcome", messages[0].Notification.Body)
assert.Equal(t, "1", messages[0].Data["a"])
assert.Equal(t, "2", messages[0].Data["b"])
assert.Equal(t, "{\"c\":\"3\",\"d\":4}", messages[0].Data["json"])

// test empty body
req = &PushNotification{
Expand Down

0 comments on commit c4fc985

Please sign in to comment.