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

Multiple recipients #18

Open
wants to merge 4 commits 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: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
client := expo.NewPushClient(nil)

// Publish message
response, err := client.Publish(
responses, err := client.Publish(
&expo.PushMessage{
To: []expo.ExponentPushToken{pushToken},
Body: "This is a test notification",
Expand All @@ -43,8 +43,10 @@ func main() {
}

// Validate responses
if response.ValidateResponse() != nil {
fmt.Println(response.PushMessage.To, "failed")
for _, res := range responses {
if res.ValidateResponse() != nil {
fmt.Println(res.PushMessage.To, "failed")
}
}
}
```
Expand Down
24 changes: 17 additions & 7 deletions sdk/push_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ func NewPushClient(config *ClientConfig) *PushClient {

// Publish sends a single push notification
// @param push_message: A PushMessage object
// @return an array of PushResponse objects which contains the results.
// @return an array of PushResponse objects which contains the results (one per each recipient).
// @return error if any requests failed
func (c *PushClient) Publish(message *PushMessage) (PushResponse, error) {
func (c *PushClient) Publish(message *PushMessage) ([]PushResponse, error) {
responses, err := c.PublishMultiple([]PushMessage{*message})
if err != nil {
return PushResponse{}, err
return nil, err
}
return responses[0], nil
return responses, nil
}

// PublishMultiple sends multiple push notifications at once
Expand All @@ -85,6 +85,9 @@ func (c *PushClient) PublishMultiple(messages []PushMessage) ([]PushResponse, er
}

func (c *PushClient) publishInternal(messages []PushMessage) ([]PushResponse, error) {
// Used for sanity check
var expectedReceipts int = 0

// Validate the messages
for _, message := range messages {
if len(message.To) == 0 {
Expand All @@ -95,6 +98,8 @@ func (c *PushClient) publishInternal(messages []PushMessage) ([]PushResponse, er
return nil, errors.New("Invalid push token")
}
}
// There will be as many receipts as there is total recipients for each message
expectedReceipts += len(message.To)
}
url := fmt.Sprintf("%s%s/push/send", c.host, c.apiURL)
jsonBytes, err := json.Marshal(messages)
Expand Down Expand Up @@ -142,14 +147,19 @@ func (c *PushClient) publishInternal(messages []PushMessage) ([]PushResponse, er
return nil, NewPushServerError("Invalid server response", resp, r, nil)
}
// Sanity check the response
if len(messages) != len(r.Data) {
if expectedReceipts != len(r.Data) {
message := "Mismatched response length. Expected %d receipts but only received %d"
errorMessage := fmt.Sprintf(message, len(messages), len(r.Data))
return nil, NewPushServerError(errorMessage, resp, r, nil)
}
// Add the original message to each response for reference
for i := range r.Data {
r.Data[i].PushMessage = messages[i]
i := 0
for _, msg := range messages {
for _, to := range msg.To {
r.Data[i].PushMessage = msg
r.Data[i].PushMessage.To = []ExponentPushToken{to}
i += 1
}
}
return r.Data, nil
}
Expand Down