-
Notifications
You must be signed in to change notification settings - Fork 68
Feat: Add OneSignal push provider adapter (#7726) #131
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,119 @@ | ||||||||||||||||||||
| <?php | ||||||||||||||||||||
|
|
||||||||||||||||||||
| namespace Utopia\Messaging\Adapter\Push; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| use Utopia\Messaging\Adapter\Push as PushAdapter; | ||||||||||||||||||||
| use Utopia\Messaging\Messages\Push as PushMessage; | ||||||||||||||||||||
| use Utopia\Messaging\Priority; | ||||||||||||||||||||
| use Utopia\Messaging\Response; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| class OneSignal extends PushAdapter | ||||||||||||||||||||
| { | ||||||||||||||||||||
| protected const NAME = 'OneSignal'; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * @param string $appId OneSignal App ID | ||||||||||||||||||||
| * @param string $apiKey OneSignal REST API Key | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| public function __construct( | ||||||||||||||||||||
| private string $appId, | ||||||||||||||||||||
| private string $apiKey, | ||||||||||||||||||||
| ) { | ||||||||||||||||||||
| parent::__construct(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * Get adapter name. | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| public function getName(): string | ||||||||||||||||||||
| { | ||||||||||||||||||||
| return static::NAME; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * Get max messages per request. | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| public function getMaxMessagesPerRequest(): int | ||||||||||||||||||||
| { | ||||||||||||||||||||
| return 2000; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /** | ||||||||||||||||||||
| * {@inheritdoc} | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| protected function process(PushMessage $message): array | ||||||||||||||||||||
| { | ||||||||||||||||||||
| $payload = [ | ||||||||||||||||||||
| 'app_id' => $this->appId, | ||||||||||||||||||||
| 'include_player_ids' => $message->getTo(), | ||||||||||||||||||||
| ]; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (!\is_null($message->getTitle())) { | ||||||||||||||||||||
| $payload['headings'] = ['en' => $message->getTitle()]; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (!\is_null($message->getBody())) { | ||||||||||||||||||||
| $payload['contents'] = ['en' => $message->getBody()]; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (!\is_null($message->getData())) { | ||||||||||||||||||||
| $payload['data'] = $message->getData(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+57
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The common
Suggested change
|
||||||||||||||||||||
| if (!\is_null($message->getAction())) { | ||||||||||||||||||||
| $payload['url'] = $message->getAction(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (!\is_null($message->getImage())) { | ||||||||||||||||||||
| $payload['big_picture'] = $message->getImage(); | ||||||||||||||||||||
| $payload['ios_attachments'] = ['id' => $message->getImage()]; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (!\is_null($message->getIcon())) { | ||||||||||||||||||||
| $payload['small_icon'] = $message->getIcon(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (!\is_null($message->getColor())) { | ||||||||||||||||||||
| $payload['android_accent_color'] = $message->getColor(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (!\is_null($message->getTag())) { | ||||||||||||||||||||
| $payload['android_group'] = $message->getTag(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (!\is_null($message->getSound())) { | ||||||||||||||||||||
| $payload['android_sound'] = $message->getSound(); | ||||||||||||||||||||
| $payload['ios_sound'] = $message->getSound() . '.wav'; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+76
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other push adapters pass the message sound through unchanged, and callers may pass values like
Suggested change
|
||||||||||||||||||||
| if (!\is_null($message->getBadge())) { | ||||||||||||||||||||
| $payload['ios_badgeType'] = 'SetTo'; | ||||||||||||||||||||
| $payload['ios_badgeCount'] = $message->getBadge(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
greptile-apps[bot] marked this conversation as resolved.
greptile-apps[bot] marked this conversation as resolved.
|
||||||||||||||||||||
| if (!\is_null($message->getContentAvailable())) { | ||||||||||||||||||||
| $payload['content_available'] = true; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+84
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The guard |
||||||||||||||||||||
| if (!\is_null($message->getPriority())) { | ||||||||||||||||||||
| $payload['priority'] = match ($message->getPriority()) { | ||||||||||||||||||||
| Priority::HIGH => 10, | ||||||||||||||||||||
| Priority::NORMAL => 5, | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| $results = $this->request( | ||||||||||||||||||||
| method: 'POST', | ||||||||||||||||||||
| url: 'https://onesignal.com/api/v1/notifications', | ||||||||||||||||||||
| headers: [ | ||||||||||||||||||||
| 'Content-Type: application/json', | ||||||||||||||||||||
| "Authorization: Basic {$this->apiKey}", | ||||||||||||||||||||
| ], | ||||||||||||||||||||
| body: $payload | ||||||||||||||||||||
| ); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| $response = new Response($this->getType()); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if ($results['statusCode'] >= 200 && $results['statusCode'] < 300) { | ||||||||||||||||||||
| $response->setDeliveredTo(\count($message->getTo())); | ||||||||||||||||||||
| foreach ($message->getTo() as $to) { | ||||||||||||||||||||
| $response->addResult($to); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
greptile-apps[bot] marked this conversation as resolved.
|
||||||||||||||||||||
| } else { | ||||||||||||||||||||
| foreach ($message->getTo() as $to) { | ||||||||||||||||||||
| $response->addResult($to, $results['response']['errors'][0] ?? 'Unknown error'); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return $response->toArray(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Tests\Adapter\Push; | ||
|
|
||
| use Utopia\Messaging\Adapter\Push\OneSignal as OneSignalAdapter; | ||
|
|
||
| class OneSignalTest extends Base | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $appId = \getenv('ONESIGNAL_APP_ID'); | ||
| $apiKey = \getenv('ONESIGNAL_API_KEY'); | ||
|
|
||
| $this->adapter = new OneSignalAdapter($appId, $apiKey); | ||
| } | ||
|
|
||
| protected function getTo(): array | ||
| { | ||
| return [\getenv('ONESIGNAL_TO')]; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.