mingalevme/onesignal
最新稳定版本:v2.0.0
Composer 安装命令:
composer require mingalevme/onesignal
包简介
Simple OneSignal PHP Client
关键字:
README 文档
README
Really simple and well tested OneSignal client (creating notifications only) with PSR-only dependencies:
- PHP 7.4+
- PSR-17: HTTP Factories
- PSR-18: HTTP Client
- JSON-extension (as of PHP 8.0.0, the JSON-extension is a core PHP extension, so it is always available)
More about code quality:
- 95%+ of code are covered with tests
- project uses Psalm and PHPStan at max levels
composer auditon every commit by CI/CD- PHP_CodeSniffer
Composer
composer require mingalevme/onesignal:^2.0.0-alpha
Examples
Example (DI Container)
Set up some DI container somewhere like Application-level service provider:
<?php declare(strict_types=1); use Mingalevme\OneSignal\ClientFactory; use Mingalevme\OneSignal\ClientFactoryInterface; use Mingalevme\OneSignal\ClientInterface; use Mingalevme\OneSignal\CreateClientOptions; class AppServiceProvider { public function register(SomeConfigClass $someConfig, SomeDIContainerClass $container): void { // ... /** @var string $appId */ $appId = $someConfig->get('onesignal-app-id'); /** @var string $restApiKey */ $restApiKey = $someConfig->get('my-rest-api-key'); $container->set(ClientFactoryInterface::class, ClientFactory::class); $container->set( ClientInterface::class, fn() => $container->get(ClientFactoryInterface::class) ->create(CreateClientOptions::new($appId, $restAPIKey)) ); // ... } }
Some application logic
Note It's recommended to use some app-level interface for notification sender, e.g.
MyAppNotificationSenderInterface, instead ofClientInterface/CreateNotificationInterface.
<?php declare(strict_types=1); use Mingalevme\OneSignal\ClientInterface; use Mingalevme\OneSignal\CreateNotificationOptions; use Mingalevme\OneSignal\Notification\PushNotification; class NotifySubscribersOnPostCreatedEventHandler { private SubscribersRespo $repo; private ClientInterface $client; public function __construct(SubscribersRepo $repo, ClientInterface $client) { $this->repo = $repo; $this->client = $client; } public function handle(PostCreatedEvent $event): void { $post = $event->getPost(); /** @var string[] $subscriberIds */ $subscriberIds = $this->repo->getSubscriberIdsOfAuthor($post->getAuthorId()); $notification = PushNotification::createContentsNotification($post->getTitle()) ->setHeadings($post->getAuthorDisplayName()) ->setData([ 'post_id' => $post->getId(), ]) ->setExternalId("post-{$post->getId()}") ->setIncludeExternalUserIds($subscriberIds); $this->client->createNotification($notification); } }
Example (Inline)
<?php declare(strict_types=1); use GuzzleHttp\Psr7\HttpFactory; use Mingalevme\OneSignal\Client; use Mingalevme\OneSignal\CreateClientOptions; use Mingalevme\OneSignal\CreateNotificationOptions; use Mingalevme\OneSignal\Notification\PushNotification; $appId = 'my-app-id'; $restApiKey = 'my-rest-api-key'; $psrHttpClient = new \GuzzleHttp\Client(); $psr7Factory = new HttpFactory(); $client = new Client(CreateClientOptions::new($appId, $restAPIKey), $psrHttpClient, $psr7Factory, $psr7Factory); $notification = PushNotification::createContentsNotification('text') ->setData([ 'type' => 'my-notification-type', 'data' => 'some-extra-data', ]) ->addFilterTagExists('tag1') ->addFilterTag('tag2', '>', 'value2') ->setSmallIcon('push_icon') ->setTtl(3600 * 3) ->setIosCategory('my-ios-category') ->setExternalId('custom-notification-id'); $result = $client->createNotification($notification); echo <<<END Notification has been successfully sent to OneSignal: #{$result->getNotificationId()}. END;
Quality (OSX)
Install PHP 7.4+
brew install php@7.4 /usr/local/opt/php@7.4/bin/pecl install xdebug
Composer
brew install composer /usr/local/opt/php@7.4/bin/php /usr/local/bin/composer install
PHPUnit
/usr/local/opt/php@7.4/bin/php vendor/bin/phpunit
Psalm
/usr/local/opt/php@7.4/bin/php vendor/bin/psalm
PHPStan
/usr/local/opt/php@7.4/bin/php vendor/bin/phpstan analyse
PHP_CodeSniffer
/usr/local/opt/php@7.4/bin/php vendor/bin/phpcs
统计信息
- 总下载量: 2.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-09