From 1344888148d6b5c04d88edbe42de965035ed4d11 Mon Sep 17 00:00:00 2001 From: Adri Van Houdt Date: Mon, 21 Sep 2015 11:18:57 +0200 Subject: [PATCH 1/3] added notification ability fixes #9 --- README.md | 3 ++- message.go | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 89886a3..bce6b1b 100644 --- a/README.md +++ b/README.md @@ -38,8 +38,9 @@ import ( func main() { // Create the message to be sent. data := map[string]interface{}{"score": "5x1", "time": "15:10"} + notification := map[string]interface{}{"title": "Notification Title", "message": "This is the body of the notification"} regIDs := []string{"4", "8", "15", "16", "23", "42"} - msg := gcm.NewMessage(data, regIDs...) + msg := gcm.NewMessage(data, regIDs..., notification) // Create a Sender to send the message. sender := &gcm.Sender{ApiKey: "sample_api_key"} diff --git a/message.go b/message.go index b63c018..9e98921 100644 --- a/message.go +++ b/message.go @@ -8,6 +8,7 @@ type Message struct { RegistrationIDs []string `json:"registration_ids"` CollapseKey string `json:"collapse_key,omitempty"` Data map[string]interface{} `json:"data,omitempty"` + Notification map[string]interface{} `json:"notification,omitempty"` DelayWhileIdle bool `json:"delay_while_idle,omitempty"` TimeToLive int `json:"time_to_live,omitempty"` RestrictedPackageName string `json:"restricted_package_name,omitempty"` @@ -16,6 +17,6 @@ type Message struct { // NewMessage returns a new Message with the specified payload // and registration IDs. -func NewMessage(data map[string]interface{}, regIDs ...string) *Message { - return &Message{RegistrationIDs: regIDs, Data: data} +func NewMessage(data map[string]interface{}, regIDs ...string, notification map[string]interface{}) *Message { + return &Message{RegistrationIDs: regIDs, Data: data, Notification: notification} } From 38b83bb6b7602eb359dbfc69b11affa48a422638 Mon Sep 17 00:00:00 2001 From: Adri Van Houdt Date: Wed, 7 Oct 2015 10:53:28 +0200 Subject: [PATCH 2/3] added custom struct for notification // added new function to deal with notification since it is optional // added basic test --- README.md | 9 +++++++-- message.go | 25 ++++++++++++++++++++++--- sender_test.go | 10 ++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bce6b1b..c742050 100644 --- a/README.md +++ b/README.md @@ -38,9 +38,14 @@ import ( func main() { // Create the message to be sent. data := map[string]interface{}{"score": "5x1", "time": "15:10"} - notification := map[string]interface{}{"title": "Notification Title", "message": "This is the body of the notification"} regIDs := []string{"4", "8", "15", "16", "23", "42"} - msg := gcm.NewMessage(data, regIDs..., notification) + msg := gcm.NewMessage(data, regIDs...) + + // or with notification + data := map[string]interface{}{"score": "5x1", "time": "15:10"} + regIDs := []string{"4", "8", "15", "16", "23", "42"} + notification := Notification{title: "Notification Title", body: "This is the body of the notification", icon: "myicon"} + msg := gcm.NewMessageWithNotification(data, notification regIDs...) // Create a Sender to send the message. sender := &gcm.Sender{ApiKey: "sample_api_key"} diff --git a/message.go b/message.go index 9e98921..a6abd7f 100644 --- a/message.go +++ b/message.go @@ -8,15 +8,34 @@ type Message struct { RegistrationIDs []string `json:"registration_ids"` CollapseKey string `json:"collapse_key,omitempty"` Data map[string]interface{} `json:"data,omitempty"` - Notification map[string]interface{} `json:"notification,omitempty"` + Notification Notification `json:"notification,omitempty"` DelayWhileIdle bool `json:"delay_while_idle,omitempty"` TimeToLive int `json:"time_to_live,omitempty"` RestrictedPackageName string `json:"restricted_package_name,omitempty"` DryRun bool `json:"dry_run,omitempty"` } +type Notification struct { + title string `json:"title"` + body string `json:"body,omitempty"` + icon string `json:"icon,omitempty"` + sound string `json:"sound,omitempty"` + badge string `json:"badge,omitempty"` + tag string `json:"tag,omitempty"` + color string `json:"color,omitempty"` + clickAction string `json:"click_action,omitempty"` + bodyLocKey string `json:"body_loc_key,omitempty"` + bodyLocArgs string `json:"body_loc_args,omitempty"` + titleLocKey string `json:"title_loc_key,omitempty"` + titleLocArgs string `json:"title_loc_args,omitempty"` +} + // NewMessage returns a new Message with the specified payload // and registration IDs. -func NewMessage(data map[string]interface{}, regIDs ...string, notification map[string]interface{}) *Message { - return &Message{RegistrationIDs: regIDs, Data: data, Notification: notification} +func NewMessage(data map[string]interface{}, regIDs ...string) *Message { + return &Message{RegistrationIDs: regIDs, Data: data} } + +func NewMessageWithNotification(data map[string]interface{}, notification Notification, regIDs ...string) *Message { + return &Message{RegistrationIDs: regIDs, Data: data, Notification: notification} +} \ No newline at end of file diff --git a/sender_test.go b/sender_test.go index 1b2c359..a6cd684 100644 --- a/sender_test.go +++ b/sender_test.go @@ -101,6 +101,16 @@ func TestSendInvalidMessage(t *testing.T) { } } +func TestNotificationCreation(t *testing.T) { + server := startTestServer(t, &testResponse{Response: &Response{}}) + defer server.Close() + sender := &Sender{ApiKey: "test"} + msg := NewMessageWithNotification(map[string]interface{}{"key": "value"}, Notification{title: "title", body: "body"}, "1") + if _, err := sender.SendNoRetry(msg); err != nil { + t.Fatalf("test failed with error: %s", err) + } +} + func TestSendNoRetrySuccess(t *testing.T) { server := startTestServer(t, &testResponse{Response: &Response{}}) defer server.Close() From a97e3753554ee008ad2d9d54c9780da3e1ad7e1f Mon Sep 17 00:00:00 2001 From: Adri Van Houdt Date: Wed, 7 Oct 2015 11:04:59 +0200 Subject: [PATCH 3/3] capital case struct props --- message.go | 24 ++++++++++++------------ sender_test.go | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/message.go b/message.go index a6abd7f..9b71aae 100644 --- a/message.go +++ b/message.go @@ -16,18 +16,18 @@ type Message struct { } type Notification struct { - title string `json:"title"` - body string `json:"body,omitempty"` - icon string `json:"icon,omitempty"` - sound string `json:"sound,omitempty"` - badge string `json:"badge,omitempty"` - tag string `json:"tag,omitempty"` - color string `json:"color,omitempty"` - clickAction string `json:"click_action,omitempty"` - bodyLocKey string `json:"body_loc_key,omitempty"` - bodyLocArgs string `json:"body_loc_args,omitempty"` - titleLocKey string `json:"title_loc_key,omitempty"` - titleLocArgs string `json:"title_loc_args,omitempty"` + Title string `json:"title"` + Body string `json:"body,omitempty"` + Icon string `json:"icon,omitempty"` + Sound string `json:"sound,omitempty"` + Badge string `json:"badge,omitempty"` + Tag string `json:"tag,omitempty"` + Color string `json:"color,omitempty"` + ClickAction string `json:"click_action,omitempty"` + BodyLocKey string `json:"body_loc_key,omitempty"` + BodyLocArgs string `json:"body_loc_args,omitempty"` + TitleLocKey string `json:"title_loc_key,omitempty"` + TitleLocArgs string `json:"title_loc_args,omitempty"` } // NewMessage returns a new Message with the specified payload diff --git a/sender_test.go b/sender_test.go index a6cd684..eac0696 100644 --- a/sender_test.go +++ b/sender_test.go @@ -105,7 +105,7 @@ func TestNotificationCreation(t *testing.T) { server := startTestServer(t, &testResponse{Response: &Response{}}) defer server.Close() sender := &Sender{ApiKey: "test"} - msg := NewMessageWithNotification(map[string]interface{}{"key": "value"}, Notification{title: "title", body: "body"}, "1") + msg := NewMessageWithNotification(map[string]interface{}{"key": "value"}, Notification{Title: "title", Body: "body"}, "1") if _, err := sender.SendNoRetry(msg); err != nil { t.Fatalf("test failed with error: %s", err) }