Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

allow notifications on gcm fixes #70 #71

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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