mingalevme/onesignal

简单的OneSignal PHP客户端

v2.0.0 2023-09-08 12:16 UTC

This package is auto-updated.

Last update: 2024-09-08 14:32:03 UTC


README

quality codecov version license

非常简单且经过良好测试的OneSignal客户端(仅创建通知),具有PSR-only依赖项

  • PHP 7.4+
  • PSR-17: HTTP Factories
  • PSR-18: HTTP Client
  • JSON扩展(自PHP 8.0.0起,JSON扩展是PHP的核心扩展,因此始终可用)

更多关于代码质量的信息

  • 95%+的代码经过测试覆盖
  • 项目使用 PsalmPHPStan最大 级别
  • CI/CD在每个提交上执行 composer audit
  • PHP_CodeSniffer

Composer

composer require mingalevme/onesignal:^2.0.0-alpha

示例

示例(依赖注入容器)

在某个位置设置一些DI容器,例如在应用层服务提供者中

<?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))
        );
        // ...
    }
}

一些应用逻辑

注意 建议使用一些应用级接口进行通知发送,例如 MyAppNotificationSenderInterface,而不是 ClientInterface/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);
    }
}

示例(内联)

<?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;

质量(OSX)

安装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