From 66c16f40a836f7fc6aab9f61b18546a152f73d4b Mon Sep 17 00:00:00 2001 From: Joe FRANCOIS Date: Tue, 5 Mar 2024 18:02:55 +0100 Subject: [PATCH] feat(config): makes the push notifications per request limit configurable --- README.md | 3 +++ config/expo-notifications.php | 4 ++++ src/ExpoNotificationsService.php | 8 +++++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6de3ae2..26e7d48 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,9 @@ You must publish the configuration file with: php artisan vendor:publish --provider="YieldStudio\LaravelExpoNotifier\ExpoNotificationsServiceProvider" --tag="expo-notifications-config" --tag="expo-notifications-migration" ``` +### Available environment variables +- `EXPO_PUSH_NOTIFICATIONS_PER_REQUEST_LIMIT` : sets the max notifications sent on a bulk request. [The official documentation says the limit should be 100](https://docs.expo.dev/push-notifications/sending-notifications/#request-errors) but in fact it's failing. You can tweak it by setting a value under 100. + ## Usage ### Send notification diff --git a/config/expo-notifications.php b/config/expo-notifications.php index 80032a8..4993152 100644 --- a/config/expo-notifications.php +++ b/config/expo-notifications.php @@ -25,5 +25,9 @@ 'service' => [ 'api_url' => 'https://exp.host/--/api/v2/push', 'host' => 'exp.host', + 'limits' => [ + // https://docs.expo.dev/push-notifications/sending-notifications/#request-errors + 'push_notifications_per_request' => (int) env('EXPO_PUSH_NOTIFICATIONS_PER_REQUEST_LIMIT', 99), + ], ], ]; diff --git a/src/ExpoNotificationsService.php b/src/ExpoNotificationsService.php index 4bc079d..1208b2e 100644 --- a/src/ExpoNotificationsService.php +++ b/src/ExpoNotificationsService.php @@ -21,8 +21,6 @@ final class ExpoNotificationsService implements ExpoNotificationsServiceInterface { - public const PUSH_NOTIFICATIONS_PER_REQUEST_LIMIT = 100; - public const SEND_NOTIFICATION_ENDPOINT = '/send'; private PendingRequest $http; @@ -35,12 +33,16 @@ final class ExpoNotificationsService implements ExpoNotificationsServiceInterfac private Collection $tickets; + private int $pushNotificationsPerRequestLimit; + public function __construct( string $apiUrl, string $host, protected readonly ExpoPendingNotificationStorageInterface $notificationStorage, protected readonly ExpoTicketStorageInterface $ticketStorage ) { + $this->pushNotificationsPerRequestLimit = config('expo-notifications.service.limits.push_notifications_per_request'); + $this->http = Http::withHeaders([ 'host' => $host, 'accept' => 'application/json', @@ -148,7 +150,7 @@ private function prepareNotificationsToSendNow(): ExpoNotificationsService ->values(); // Splits into multiples chunks of max limitation - $this->notificationChunks = $this->notificationsToSend->chunk(self::PUSH_NOTIFICATIONS_PER_REQUEST_LIMIT); + $this->notificationChunks = $this->notificationsToSend->chunk($this->pushNotificationsPerRequestLimit); return $this; }