Skip to content

Commit

Permalink
allow notifications on gcm fixes Imgur#70
Browse files Browse the repository at this point in the history
  • Loading branch information
AdriVanHoudt committed Sep 21, 2015
1 parent bbef822 commit 34f4b62
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
```
Expand Down
14 changes: 8 additions & 6 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()

Expand Down
4 changes: 4 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func TestGCM(t *testing.T) {
"data": {
"foobar": "foo"
},
"notification" : {
"title": "Notification Title",
"body": "Notification Body"
},
"time": 1234
}
}`), &msg)
Expand Down

0 comments on commit 34f4b62

Please sign in to comment.