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 Pushwoosh Messaging adapter #31

Closed
Closed
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
99 changes: 99 additions & 0 deletions src/Utopia/Messaging/Adapters/Push/Pushwoosh.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Utopia\Messaging\Adapters\Push;

use Exception;
use JsonException;
use Utopia\Messaging\Adapters\Push as PushAdapter;
use Utopia\Messaging\Messages\Push as PushMessage;

class Pushwoosh extends PushAdapter
{
protected const SEND_MESSAGE_URL = 'https://api.pushwoosh.com/json/1.3/createMessage';

/**
* @param string $applicationId Pushwoosh application ID.
* @param string $authKey Pushwoosh auth key.
*/
public function __construct(
private string $applicationId,
private string $authKey,
) {
}

/**
* @inheritdoc
*/
public function getName(): string
{
return 'Pushwoosh';
}

/**
* @inheritdoc
*/
public function getMaxMessagesPerRequest(): int
{
return 1000;
}

/**
* @inheritdoc
*
* @throws Exception
* @throws JsonException
*/
protected function process(PushMessage $message): string
{
return $this->request(
method: 'POST',
url: static::SEND_MESSAGE_URL,
body: \json_encode(
['request' => $this->buildRequest($message)],
JSON_THROW_ON_ERROR,
),
);
}

/**
* @param PushMessage $message
* @return array<string, mixed>
*/
protected function buildRequest(PushMessage $message): array
{
$request = [
'application' => $this->applicationId,
'auth' => $this->authKey,
'notifications' => [],
];

$notification = [
'send_date' => 'now',
'ignore_user_timezone' => true,
'title' => $message->getTitle(),
'platforms' => [1,3,5,7,8,9,10,11,12,13,17],
'content' => $message->getBody(),
'wns_content' => base64_encode($message->getBody()),
'to' => $message->getTo(),
'data' => $message->getData() ?? [],
];

if ($message->getSound() !== null) {
$notification['ios_sound'] = "sound {$message->getSound()}";
$notification['android_sound'] = $message->getSound();
}

if ($message->getIcon() !== null) {
$notification['android_icon'] = 'android_icon';
}

if ($message->getBadge() !== null) {
$notification['ios_badges'] = $message->getBadge();
$notification['android_badges'] = $message->getBadge();
}

$request['notifications'][] = $notification;

return $request;
}
}
40 changes: 40 additions & 0 deletions tests/e2e/Push/PushwooshTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\E2E;

use Utopia\Messaging\Adapters\Push\Pushwoosh;
use Utopia\Messaging\Messages\Push;

class PushwooshTest extends Base
{
public function testSend(): void
{
$applicationId = getenv('PUSHUWOOSH_APP_ID');
$authKey = getenv('PUSHWOOSH_AUTH_KEY');

$adapter = new Pushwoosh($applicationId, $authKey);

$to = getenv('PUSHUWOOSH_TO_TOKEN');

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

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

$this->assertNotEmpty($response);
$this->assertEquals(200, $response->status_code);
$this->assertEquals('OK', $response->status_message);
$this->assertNotEmpty($response->response);
$this->assertCount(1, $response->response->Messages);
}
}