Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
abnegate committed Dec 15, 2023
1 parent 9035bf2 commit 9709afd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ $message = new Push(
content: 'Hello World'
);

$messaging = new FCM('YOUR_SERVER_KEY');
$messaging = new FCM('YOUR_SERVICE_ACCOUNT_JSON');
$messaging->send($message);
```

Expand Down Expand Up @@ -108,7 +108,7 @@ $messaging->send($message);

### Push
- [x] [FCM](https://firebase.google.com/docs/cloud-messaging)
- [ ] [APNS](https://developer.apple.com/documentation/usernotifications)
- [x] [APNS](https://developer.apple.com/documentation/usernotifications)
- [ ] [OneSignal](https://onesignal.com/)
- [ ] [Pusher](https://pusher.com/)
- [ ] [WebPush](https://developer.mozilla.org/en-US/docs/Web/API/Push_API)
Expand Down
19 changes: 16 additions & 3 deletions docs/add-new-adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,25 @@ public function process(Email $message): string
}
```

The base `Adapter` class includes a helper method called `request()` that can be used to send HTTP requests to the messaging provider. It accepts the following parameters, and returns the response as a string:
The base `Adapter` class includes a two helper functions called `request()` and `requestMulti()` that can be used to send HTTP requests to the messaging provider.

The `request()` function will send a single request and accepts the following parameters:

- `method` - The HTTP method to use. For example, `POST`, `GET`, `PUT`, `PATCH` or `DELETE`.
- `url` - The URL to send the request to.
- `headers` - An array of headers to send with the request.
- `body` - The body of the request. It can be either a string or an array.
- `body` - The body of the request as a string, or null if no body is required.
- `timeout` - The timeout in seconds for the request.

The `requestMulti()` function will send multiple concurrent requests via HTTP/2 multiplexing, and accepts the following parameters:

- `method` - The HTTP method to use. For example, `POST`, `GET`, `PUT`, `PATCH` or `DELETE`.
- `urls` - An array of URLs to send the requests to.
- `headers` - An array of headers to send with the requests.
- `bodies` - An array of bodies of the requests as strings, or an empty array if no body is required.
- `timeout` - The timeout in seconds for the requests.

`urls` and `bodies` must either be the same length, or one of them must contain only a single element. If `urls` contains only a single element, it will be used for all requests. If `bodies` contains only a single element, it will be used for all requests.

The default content type of the request is `x-www-form-urlencoded`, but you can change it by adding a `Content-Type` header. No encoding is applied to the body, so you need to make sure it is encoded properly before sending the request.

Expand Down Expand Up @@ -144,7 +157,7 @@ If everything goes well, raise a pull request and be ready to respond to any fee

## 4. Raise a pull request

First of all, commit the changes with the message `Added YYY Storage adapter` and push it. This will publish a new branch to your forked version of `utopia-php/messaging`. If you visit it at `github.com/YOUR_USERNAME/messaging`, you will see a new alert saying you are ready to submit a pull request. Follow the steps GitHub provides, and at the end, you will have your pull request submitted.
First of all, commit the changes with the message `Added YYY adapter` and push it. This will publish a new branch to your forked version of `utopia-php/messaging`. If you visit it at `github.com/YOUR_USERNAME/messaging`, you will see a new alert saying you are ready to submit a pull request. Follow the steps GitHub provides, and at the end, you will have your pull request submitted.

## 🤕 Stuck ?
If you need any help with the contribution, feel free to head over to [our discord channel](https://appwrite.io/discord) and we'll be happy to help you out.
39 changes: 32 additions & 7 deletions src/Utopia/Messaging/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ abstract public function getMaxMessagesPerRequest(): int;
/**
* Send a message.
*
* @return array{deliveredTo: int, type: string, results: array<array<string, mixed>>}|array<string, array{deliveredTo: int, type: string, results: array<array<string, mixed>>}>
* @return array{
* deliveredTo: int,
* type: string,
* results: array<array<string, mixed>>
* } | array<string, array{
* deliveredTo: int,
* type: string,
* results: array<array<string, mixed>>
* }>
*
* @throws \Exception
*/
Expand All @@ -47,13 +55,20 @@ public function send(Message $message): array
}

/**
* Send an HTTP request.
* Send a single HTTP request.
*
* @param string $method The HTTP method to use.
* @param string $url The URL to send the request to.
* @param array<string> $headers An array of headers to send with the request.
* @param string|null $body The body of the request.
* @return array{url: string, statusCode: int, response: array<string, mixed>|null, error: string|null}
* @param int $timeout The timeout in seconds.
*
* @return array{
* url: string,
* statusCode: int,
* response: array<string, mixed>|null,
* error: string|null
* }
*
* @throws \Exception If the request fails.
*/
Expand Down Expand Up @@ -99,10 +114,20 @@ protected function request(
}

/**
* @param array<string> $urls
* @param array<string> $headers
* @param array<string> $bodies
* @return array<array{url: string, statusCode: int, response: array<string, mixed>|null, error: string|null}>
* Send multiple concurrent HTTP requests using HTTP/2 multiplexing.
*
* @param string $method
* @param array<string> $urls
* @param array<string> $headers
* @param array<string> $bodies
* @param int $timeout
*
* @return array<array{
* url: string,
* statusCode: int,
* response: array<string, mixed>|null,
* error: string|null
* }>
*
* @throws \Exception
*/
Expand Down
18 changes: 4 additions & 14 deletions src/Utopia/Messaging/Adapter/Push/FCM.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ protected function process(PushMessage $message): array
$signingKey,
$signingAlgorithm,
);

$signingKey = null;
$payload = null;

$token = $this->request(
method: 'POST',
Expand Down Expand Up @@ -143,23 +146,10 @@ protected function process(PushMessage $message): array
}
$response->addResultForRecipient(
$message->getTo()[$index],
$this->getSpecificErrorMessage($result['error'])
$result['response']['error']['message'] ?? ''
);
}

return $response->toArray();
}

private function getSpecificErrorMessage(string $error): string
{
return match ($error) {
'MissingRegistration' => 'Bad Request. Missing token.',
'InvalidRegistration' => 'Invalid device token.',
'NotRegistered' => 'Expired device token.',
'MessageTooBig' => 'Payload is too large. Messages must be less than 4096 bytes.',
'DeviceMessageRateExceeded' => 'Too many requests were made to the same device token.',
'InternalServerError' => 'Internal server error.',
default => $error,
};
}
}

0 comments on commit 9709afd

Please sign in to comment.