diff --git a/README.md b/README.md index 89886a3..c742050 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,12 @@ func main() { data := map[string]interface{}{"score": "5x1", "time": "15:10"} regIDs := []string{"4", "8", "15", "16", "23", "42"} 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 b63c018..9b71aae 100644 --- a/message.go +++ b/message.go @@ -8,14 +8,34 @@ type Message struct { RegistrationIDs []string `json:"registration_ids"` CollapseKey string `json:"collapse_key,omitempty"` Data map[string]interface{} `json:"data,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) *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..eac0696 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()