From 34f4b6207a40773dfe09f2bc6e1a8d7aef993dc5 Mon Sep 17 00:00:00 2001 From: Adri Van Houdt Date: Mon, 21 Sep 2015 11:39:05 +0200 Subject: [PATCH 1/2] allow notifications on gcm fixes #70 --- README.md | 7 ++++--- message.go | 14 ++++++++------ message_test.go | 4 ++++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 75326bd..fae5871 100644 --- a/README.md +++ b/README.md @@ -97,9 +97,10 @@ Multiple registration ids can be listed in the same command "registration_ids" : string -- one or more registration ids separated by commas }, "message" : { - "event" : string, - "data" : object, - "time" : int + "event" : string, + "data" : object, + "notification": object, + "time" : int } } ``` diff --git a/message.go b/message.go index df5cbfd..6af48de 100644 --- a/message.go +++ b/message.go @@ -18,9 +18,10 @@ type CommandMsg struct { } type Message struct { - Event string `json:"event"` - Data map[string]interface{} `json:"data"` - Time int64 `json:"time"` + Event string `json:"event"` + Data map[string]interface{} `json:"data"` + Notification map[string]interface{} `json:"notification"` + Time int64 `json:"time"` } func (this *CommandMsg) FromSocket(sock *Socket) { @@ -163,12 +164,13 @@ func (this *CommandMsg) FromRedis(server *Server) { func (this *CommandMsg) formatMessage() (*Message, error) { event, e_ok := this.Message["event"].(string) data, b_ok := this.Message["data"].(map[string]interface{}) + notification, c_ok := this.Message["notification"].(map[string]interface{}) - if !b_ok || !e_ok { + if !b_ok || !e_ok { // notification is optional return nil, errors.New("Could not format message") } - msg := &Message{event, data, time.Now().UTC().Unix()} + msg := &Message{event, data, notification, time.Now().UTC().Unix()} return msg, nil } @@ -249,7 +251,7 @@ func (this *CommandMsg) pushAndroid(server *Server) { data := map[string]interface{}{"event": msg.Event, "data": msg.Data, "time": msg.Time} regIDs := strings.Split(registration_ids, ",") - gcmMessage := gcm.NewMessage(data, regIDs...) + gcmMessage := gcm.NewMessage(data, regIDs..., msg.notification) sender := server.GetGCMClient() diff --git a/message_test.go b/message_test.go index 4942e5b..35415dc 100644 --- a/message_test.go +++ b/message_test.go @@ -78,6 +78,10 @@ func TestGCM(t *testing.T) { "data": { "foobar": "foo" }, + "notification" : { + "title": "Notification Title", + "body": "Notification Body" + }, "time": 1234 } }`), &msg) From 6a4846fbaefeaad4c4df7c456ae9e3aa583ab208 Mon Sep 17 00:00:00 2001 From: Adri Van Houdt Date: Mon, 21 Sep 2015 11:46:57 +0200 Subject: [PATCH 2/2] typo --- message.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/message.go b/message.go index 6af48de..e8e68de 100644 --- a/message.go +++ b/message.go @@ -251,7 +251,7 @@ func (this *CommandMsg) pushAndroid(server *Server) { data := map[string]interface{}{"event": msg.Event, "data": msg.Data, "time": msg.Time} regIDs := strings.Split(registration_ids, ",") - gcmMessage := gcm.NewMessage(data, regIDs..., msg.notification) + gcmMessage := gcm.NewMessage(data, regIDs..., msg.Notification) sender := server.GetGCMClient()