Skip to content

Commit

Permalink
Merge pull request #50 from chimit/chunk
Browse files Browse the repository at this point in the history
Split recipients array into chunks to respect FCM limits
  • Loading branch information
benwilkins authored Sep 18, 2019
2 parents d49d74a + 7d1451e commit d003d6d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/FcmChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,37 @@ public function send($notifiable, Notification $notification)

$message->to($to);
}

$response_array = [];

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(),
]);

array_push($response_array, \GuzzleHttp\json_decode($response->getBody(), true));
}
} 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(),
]);

array_push($response_array, \GuzzleHttp\json_decode($response->getBody(), true));
}

return \GuzzleHttp\json_decode($response->getBody(), true);
return $response_array;
}
}
2 changes: 2 additions & 0 deletions src/FcmMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit d003d6d

Please sign in to comment.