Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Pusher Messaging Adapter #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
with:
fetch-depth: 2
- run: git checkout HEAD^2
- name: Run Tests
- name: Run Tests
env:
MAILGUN_API_KEY: ${{ secrets.MAILGUN_API_KEY }}
MAILGUN_DOMAIN: ${{ secrets.MAILGUN_DOMAIN }}
Expand Down Expand Up @@ -40,7 +40,10 @@ jobs:
VONAGE_API_SECRET: ${{ secrets.VONAGE_API_SECRET }}
VONAGE_TO: ${{ secrets.VONAGE_TO }}
VONAGE_FROM: ${{ secrets.VONAGE_FROM }}
PUSHER_INSTANCE_ID: ${{ secrets.PUSHER_INSTANCE_ID }}
PUSHER_SECRET_KEY: ${{ secrets.PUSHER_SECRET_KEY }}
PUSHER_TO: ${{ secrets.PUSHER_TO }}
run: |
docker compose up -d --build
sleep 5
docker compose exec tests vendor/bin/phpunit
docker compose exec tests vendor/bin/phpunit
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ $messaging->send($message);
- [x] [FCM](https://firebase.google.com/docs/cloud-messaging)
- [ ] [APNS](https://developer.apple.com/documentation/usernotifications)
- [ ] [OneSignal](https://onesignal.com/)
- [ ] [Pusher](https://pusher.com/)
- [x] [Pusher](https://pusher.com/)
- [ ] [WebPush](https://developer.mozilla.org/en-US/docs/Web/API/Push_API)
- [ ] [UrbanAirship](https://www.urbanairship.com/)
- [ ] [Pushwoosh](https://www.pushwoosh.com/)
Expand Down
7 changes: 5 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ services:
- VONAGE_API_SECRET
- VONAGE_TO
- VONAGE_FROM
- PUSHER_INSTANCE_ID
- PUSHER_SECRET_KEY
- PUSHER_TO
build:
context: .
volumes:
- ./src:/usr/local/src/src
- ./tests:/usr/local/src/tests
- ./phpunit.xml:/usr/local/src/phpunit.xml

maildev:
image: appwrite/mailcatcher:1.0.0
ports:
Expand All @@ -44,4 +47,4 @@ services:
request-catcher:
image: appwrite/requestcatcher:1.0.0
ports:
- '10001:5000'
- '10001:5000'
104 changes: 104 additions & 0 deletions src/Utopia/Messaging/Adapters/Push/Pusher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Utopia\Messaging\Adapters\Push;

use Utopia\Messaging\Adapters\Push as PushAdapter;
use Utopia\Messaging\Messages\Push;

class Pusher extends PushAdapter
{
/**
* @param string $instanceId The unique identifier for Pusher Beams instance.
* @param string $secretKey The secret key to access Pusher Beams instance.
*/
public function __construct(
private string $instanceId,
private string $secretKey,
) {
}

/**
* Get adapter name.
*
* @return string
*/
public function getName(): string
{
return 'Pusher';
}

/**
* Get max messages per request.
*
* @return int
*/
public function getMaxMessagesPerRequest(): int
{
return 1000;
}

private function removeNullFields(array $object): array
{
$output = [];
foreach ($object as $key => $val) {
if (is_array($val) && array_keys($val) !== range(0, count($val) - 1)) {
$output[$key] = $this->removeNullFields($val);
} elseif (! is_null($val)) {
$output[$key] = $val;
}
}

return $output;
}

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(Push $message): string
{
return $this->request(
method: 'POST',
url: "https://{$this->instanceId}.pushnotifications.pusher.com/publish_api/v1/instances/{$this->instanceId}/publishes/users",
headers: [
'Content-Type: application/json',
"Authorization: Bearer {$this->secretKey}",
],
body: \json_encode($this->removeNullFields([
'users' => $message->getTo(),
'apns' => [
'aps' => [
'alert' => [
'title' => $message->getTitle(),
'body' => $message->getBody(),
],
'badge' => $message->getBadge(),
'sound' => $message->getSound(),
'data' => $message->getData(),
],
],
'fcm' => [
'notification' => [
'title' => $message->getTitle(),
'body' => $message->getBody(),
'click_action' => $message->getAction(),
'icon' => $message->getIcon(),
'color' => $message->getColor(),
'sound' => $message->getSound(),
'tag' => $message->getTag(),
],
'data' => $message->getData(),
],
'web' => [
'notification' => [
'title' => $message->getTitle(),
'body' => $message->getBody(),
'icon' => $message->getIcon(),
],
'data' => $message->getData(),
],
]))
);
}
}
39 changes: 39 additions & 0 deletions tests/e2e/Push/PusherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tests\E2E;

use Utopia\Messaging\Adapters\Push\Pusher as PusherAdapter;
use Utopia\Messaging\Messages\Push;

class PusherTest extends Base
{
public function testSend(): void
{
$instanceId = getenv('PUSHER_INSTANCE_ID');
$secretKey = getenv('PUSHER_SECRET_KEY');

$adapter = new PusherAdapter($instanceId, $secretKey);

$to = getenv('PUSHER_TO');

$message = new Push(
to: [$to],
title: 'TestTitle',
body: 'TestBody',
data: [
'some' => 'metadata',
],
action: null,
sound: 'default',
icon: null,
color: null,
tag: null,
badge: '1'
);

$response = \json_decode($adapter->send($message));

$this->assertNotEmpty($response);
$this->assertIsString($response->publishId);
}
}
Loading