From d442b5208a92bb31b553d2e62ab8044afa5076fc Mon Sep 17 00:00:00 2001 From: Chimit Date: Thu, 5 Sep 2019 18:27:33 +0800 Subject: [PATCH 1/3] Transform recepients array with one item into a string --- src/FcmMessage.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/FcmMessage.php b/src/FcmMessage.php index 5616f62..b090155 100755 --- a/src/FcmMessage.php +++ b/src/FcmMessage.php @@ -76,6 +76,8 @@ public function to($recipient, $recipientIsTopic = false) { if ($recipientIsTopic && is_string($recipient)) { $this->to = '/topics/'.$recipient; + } elseif (is_array($recipient) && count($recipient) == 1) { + $this->to = $recipient[0]; } else { $this->to = $recipient; } From 5ec395f560db5bc0fa908184915b31bf9b9a4f97 Mon Sep 17 00:00:00 2001 From: Chimit Date: Thu, 5 Sep 2019 18:30:08 +0800 Subject: [PATCH 2/3] Split recepients arrays into chunks with 1000 items --- src/FcmChannel.php | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/FcmChannel.php b/src/FcmChannel.php index 23b0e9b..b710257 100755 --- a/src/FcmChannel.php +++ b/src/FcmChannel.php @@ -50,17 +50,30 @@ public function send($notifiable, Notification $notification) $message->to($to); } + + if (is_array($message->getTo())) { + $chunks = array_chunk($message->getTo(), 1000); - $response = $this->client->post(self::API_URI, [ - 'headers' => array_merge( - [ + foreach ($chunks as $chunk) { + $message->to($chunk); + + $response = $this->client->post(self::API_URI, [ + 'headers' => [ + 'Authorization' => 'key='.$this->apiKey, + 'Content-Type' => 'application/json', + ], + 'body' => $message->formatData(), + ]); + } + } else { + $response = $this->client->post(self::API_URI, [ + 'headers' => [ 'Authorization' => 'key='.$this->apiKey, 'Content-Type' => 'application/json', ], - $message->getHeaders() - ), - 'body' => $message->formatData(), - ]); + 'body' => $message->formatData(), + ]); + } return \GuzzleHttp\json_decode($response->getBody(), true); } From 7d1451e1105e390ceed300787b7dcf6143758adc Mon Sep 17 00:00:00 2001 From: Chimit Date: Mon, 9 Sep 2019 13:14:58 +0800 Subject: [PATCH 3/3] Put chunked responses into array --- src/FcmChannel.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/FcmChannel.php b/src/FcmChannel.php index b710257..749c10c 100755 --- a/src/FcmChannel.php +++ b/src/FcmChannel.php @@ -51,6 +51,8 @@ public function send($notifiable, Notification $notification) $message->to($to); } + $response_array = []; + if (is_array($message->getTo())) { $chunks = array_chunk($message->getTo(), 1000); @@ -64,6 +66,8 @@ public function send($notifiable, Notification $notification) ], 'body' => $message->formatData(), ]); + + array_push($response_array, \GuzzleHttp\json_decode($response->getBody(), true)); } } else { $response = $this->client->post(self::API_URI, [ @@ -73,8 +77,10 @@ public function send($notifiable, Notification $notification) ], 'body' => $message->formatData(), ]); + + array_push($response_array, \GuzzleHttp\json_decode($response->getBody(), true)); } - return \GuzzleHttp\json_decode($response->getBody(), true); + return $response_array; } }