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

add priority on message #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package gcm

const (
//HighPriority represent high value for priority field
HighPriority = "high"
//NormalPriority represent normal value for priority field
NormalPriority = "normal"
)

// Message is used by the application server to send a message to
// the GCM server. See the documentation for GCM Architectural
// Overview for more information:
Expand All @@ -12,6 +19,7 @@ type Message struct {
TimeToLive int `json:"time_to_live,omitempty"`
RestrictedPackageName string `json:"restricted_package_name,omitempty"`
DryRun bool `json:"dry_run,omitempty"`
Priority string `json:"priority,omitempty"`
}

// NewMessage returns a new Message with the specified payload
Expand Down
2 changes: 2 additions & 0 deletions sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ func checkMessage(msg *Message) error {
} else if msg.TimeToLive < 0 || 2419200 < msg.TimeToLive {
return errors.New("the message's TimeToLive field must be an integer " +
"between 0 and 2419200 (4 weeks)")
} else if len(msg.Priority) > 0 && msg.Priority != HighPriority && msg.Priority != NormalPriority {
return errors.New("the message priority value must be normal or high")
}
return nil
}
17 changes: 17 additions & 0 deletions sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func TestSendNoRetryInvalidMessage(t *testing.T) {
if _, err := sender.SendNoRetry(&Message{RegistrationIDs: []string{"1"}, TimeToLive: 2419201}); err == nil {
t.Fatal("test should fail when message TimeToLive field is greater than 2419200")
}
if _, err := sender.SendNoRetry(&Message{RegistrationIDs: []string{"1"}, Priority: "invalid"}); err == nil {
t.Fatal("test should fail when priority value is not high or normal")
}
}

func TestSendInvalidMessage(t *testing.T) {
Expand All @@ -99,6 +102,9 @@ func TestSendInvalidMessage(t *testing.T) {
if _, err := sender.Send(&Message{RegistrationIDs: []string{"1"}, TimeToLive: 2419201}, 0); err == nil {
t.Fatal("test should fail when message TimeToLive field is greater than 2419200")
}
if _, err := sender.Send(&Message{RegistrationIDs: []string{"1"}, Priority: "invalid"}, 0); err == nil {
t.Fatal("test should fail when priority value is not high or normal")
}
}

func TestSendNoRetrySuccess(t *testing.T) {
Expand Down Expand Up @@ -160,3 +166,14 @@ func TestSendOneRetryNonrecoverableFailure(t *testing.T) {
t.Fatal("send should fail after one retry")
}
}

func TestSendWithPrioritySuccess(t *testing.T) {
server := startTestServer(t, &testResponse{Response: &Response{}})
defer server.Close()
sender := &Sender{ApiKey: "test"}
msg := NewMessage(map[string]interface{}{"key": "value"}, "1")
msg.Priority = HighPriority
if _, err := sender.SendNoRetry(msg); err != nil {
t.Fatalf("test failed with error: %s", err)
}
}