Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added notification ability fixes #9 #10

Closed
wants to merge 3 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
20 changes: 20 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
10 changes: 10 additions & 0 deletions sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down