Skip to content

Commit

Permalink
Merge pull request #26 from YieldStudio/feat/configurable-push-notifi…
Browse files Browse the repository at this point in the history
…cation-limit

feat(config): makes the push notifications per request limit configurable
  • Loading branch information
joemugen authored Mar 6, 2024
2 parents 8aa6c0f + 66c16f4 commit aaa6bb6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions config/expo-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
],
];
8 changes: 5 additions & 3 deletions src/ExpoNotificationsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit aaa6bb6

Please sign in to comment.