Skip to content

Commit

Permalink
update channel to use sendMulticast without chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
ankurk91 committed Jun 27, 2023
1 parent 18fcc18 commit 3703c50
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [2.0.0](https://github.com/ankurk91/laravel-bundler/compare/1.5.1..2.0.0)

* Require `kreait/firebase-php@^7.5` due to [Discontinued FCM Messaging API](https://github.com/kreait/firebase-php/issues/804)

## [1.5.1](https://github.com/ankurk91/laravel-bundler/compare/1.5.0..1.5.1)

* Add support for Laravel 10
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"illuminate/events": "^9.34 || ^10.0",
"illuminate/notifications": "^9.34 || ^10.0",
"illuminate/support": "^9.34 || ^10.0",
"kreait/laravel-firebase": "^5.0"
"kreait/firebase-php": "^7.5",
"kreait/laravel-firebase": "^5.2"
},
"require-dev": {
"orchestra/testbench": "^7.10 || ^8.0",
Expand Down
23 changes: 5 additions & 18 deletions src/FCMChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
use Illuminate\Notifications\Notification;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Kreait\Firebase\Contract\Messaging;
use Kreait\Firebase\Contract\Messaging as MessagingClient;
use Kreait\Firebase\Exception\FirebaseException;
use Kreait\Firebase\Exception\Messaging\NotFound;
use Kreait\Firebase\Exception\MessagingException;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Http\Request\SendMessageToTokens;
use Kreait\Firebase\Messaging\MessageTarget;
use Kreait\Laravel\Firebase\Facades\Firebase;
use NotificationChannels\FCM\Exception\HttpException;
Expand All @@ -24,8 +22,6 @@

class FCMChannel
{
protected const BATCH_MESSAGE_LIMIT = Messaging::BATCH_MESSAGE_LIMIT;

public function __construct(protected Dispatcher $events)
{
//
Expand All @@ -36,11 +32,11 @@ public function __construct(protected Dispatcher $events)
*
* @param mixed $notifiable
* @param Notification $notification
* @return array<mixed>
* @return array<non-empty-string, mixed>|null|\Kreait\Firebase\Messaging\MulticastSendReport
*
* @throws FirebaseException
*/
public function send($notifiable, Notification $notification): array
public function send($notifiable, Notification $notification)
{
// Build the target
[$targetType, $targetValue] = $this->getTarget($notifiable, $notification);
Expand All @@ -50,7 +46,7 @@ public function send($notifiable, Notification $notification): array

// Check if there is a target, otherwise return an empty array
if (empty($targetValue)) {
return [];
return null;
}

// Make the messaging client
Expand All @@ -60,23 +56,14 @@ public function send($notifiable, Notification $notification): array
try {
// Send multicast
if ($this->canSendToMulticast($targetType, $targetValue)) {
$chunkedTokens = array_chunk($targetValue, self::BATCH_MESSAGE_LIMIT);

$responses = [];
foreach ($chunkedTokens as $chunkedToken) {
$responses[] = $client->sendMulticast($message, $chunkedToken);
}

return $responses;
return $client->sendMulticast($message, $targetValue);
}

// Set the target and type; since we are sure that target is single
$message = $message->withChangedTarget($targetType, Arr::first($targetValue));

// Send to single target
return [
$client->send($message)
];
return $client->send($message);
} catch (NotFound $exception) {
$this->emitFailedEvent($notifiable, $notification, $exception);

Expand Down
20 changes: 7 additions & 13 deletions tests/FCMChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Closure;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
use Kreait\Firebase\Contract\Messaging as MessagingClient;
Expand Down Expand Up @@ -86,8 +85,7 @@ public function a_message_can_be_send_to_a_target()
$response = $this->channel->send(new TestModel, new TestNotification);

$this->assertIsArray($response);
$this->assertIsArray(Arr::first($response));
$this->assertArrayHasKey('response-key', Arr::first($response));
$this->assertArrayHasKey('response-key', $response);
}

/** @test */
Expand All @@ -109,8 +107,7 @@ public function a_message_can_be_send_to_a_topic()
$response = $this->channel->send(new TestModel(MessageTarget::TOPIC), new TestNotification);

$this->assertIsArray($response);
$this->assertIsArray(Arr::first($response));
$this->assertArrayHasKey('response-key', Arr::first($response));
$this->assertArrayHasKey('response-key', $response);
}

/** @test */
Expand All @@ -126,14 +123,13 @@ public function a_message_can_be_send_to_a_condition()
], $this->getPropertyValue($message, 'notification')->jsonSerialize());

return true;
})->andReturn(['response-key' => 2]);
})->andReturn(['response-key' => 3]);
});

$response = $this->channel->send(new TestModel(MessageTarget::CONDITION), new TestNotification);

$this->assertIsArray($response);
$this->assertIsArray(Arr::first($response));
$this->assertArrayHasKey('response-key', Arr::first($response));
$this->assertArrayHasKey('response-key', $response);
}

/** @test */
Expand All @@ -152,8 +148,7 @@ public function a_message_can_be_send_to_multicast()

$response = $this->channel->send(new TestModel(MessageTarget::TOKEN, true), new TestNotification);

$this->assertIsArray($response);
$this->assertInstanceOf(MulticastSendReport::class, Arr::first($response));
$this->assertInstanceOf(MulticastSendReport::class, $response);
}

/** @test */
Expand Down Expand Up @@ -189,8 +184,7 @@ public function nothing_is_sent_when_no_token_is_supplied()
{
$response = $this->channel->send(new TestModel(null), new TestNotification);

$this->assertIsArray($response);
$this->assertEmpty($response);
$this->assertNull($response);
}

/** @test */
Expand All @@ -206,7 +200,7 @@ public function it_supports_anonymous_notifiable()
], $this->getPropertyValue($message, 'notification')->jsonSerialize());

return true;
})->andReturn(['response-key' => 1]);
})->andReturn(['response-key' => 5]);
});

Event::fake();
Expand Down

0 comments on commit 3703c50

Please sign in to comment.