From 68f083008177f7745f25ab0bd089f707da5f4097 Mon Sep 17 00:00:00 2001 From: Grzegorz Kaczmarek Date: Mon, 16 Aug 2021 18:59:51 +0200 Subject: [PATCH 1/4] fix multiple recipients in one message --- sdk/push_client.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/sdk/push_client.go b/sdk/push_client.go index f5e7f0b..aa3d444 100644 --- a/sdk/push_client.go +++ b/sdk/push_client.go @@ -68,12 +68,12 @@ func NewPushClient(config *ClientConfig) *PushClient { // @param push_message: A PushMessage object // @return an array of PushResponse objects which contains the results. // @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 @@ -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 exptectedReceipts int = 0 + // Validate the messages for _, message := range messages { if len(message.To) == 0 { @@ -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 + exptectedReceipts += len(message.To) } url := fmt.Sprintf("%s%s/push/send", c.host, c.apiURL) jsonBytes, err := json.Marshal(messages) @@ -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 exptectedReceipts != 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 } From b4f2664f8cb716514ec5a3268a2d97ece7d5fa19 Mon Sep 17 00:00:00 2001 From: Grzegorz Kaczmarek Date: Mon, 16 Aug 2021 19:14:22 +0200 Subject: [PATCH 2/4] comment modified publish --- sdk/push_client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/push_client.go b/sdk/push_client.go index aa3d444..8aa4a99 100644 --- a/sdk/push_client.go +++ b/sdk/push_client.go @@ -66,7 +66,7 @@ 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) { responses, err := c.PublishMultiple([]PushMessage{*message}) From 11149332097da740f1adb95d7050d1c7690a4a06 Mon Sep 17 00:00:00 2001 From: Grzegorz Kaczmarek Date: Mon, 16 Aug 2021 19:14:32 +0200 Subject: [PATCH 3/4] update README --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 214e712..2253137 100644 --- a/README.md +++ b/README.md @@ -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", @@ -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") + } } } ``` From 280ba35546ca22fcaf4012a74041a276321468a3 Mon Sep 17 00:00:00 2001 From: navivix Date: Tue, 12 Oct 2021 20:59:21 +0200 Subject: [PATCH 4/4] fix typo --- sdk/push_client.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/push_client.go b/sdk/push_client.go index 8aa4a99..04cb0c2 100644 --- a/sdk/push_client.go +++ b/sdk/push_client.go @@ -86,7 +86,7 @@ func (c *PushClient) PublishMultiple(messages []PushMessage) ([]PushResponse, er func (c *PushClient) publishInternal(messages []PushMessage) ([]PushResponse, error) { // Used for sanity check - var exptectedReceipts int = 0 + var expectedReceipts int = 0 // Validate the messages for _, message := range messages { @@ -99,7 +99,7 @@ func (c *PushClient) publishInternal(messages []PushMessage) ([]PushResponse, er } } // There will be as many receipts as there is total recipients for each message - exptectedReceipts += len(message.To) + expectedReceipts += len(message.To) } url := fmt.Sprintf("%s%s/push/send", c.host, c.apiURL) jsonBytes, err := json.Marshal(messages) @@ -147,7 +147,7 @@ func (c *PushClient) publishInternal(messages []PushMessage) ([]PushResponse, er return nil, NewPushServerError("Invalid server response", resp, r, nil) } // Sanity check the response - if exptectedReceipts != 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)