norkunas / onesignal-php-api
PHP 的 OneSignal API
Requires
- php: >=7.3
- ext-json: *
- psr/http-client: ^1.0
- psr/http-client-implementation: ^1.0
- psr/http-factory: ^1.0
- psr/http-factory-implementation: ^1.0
- symfony/deprecation-contracts: ^2.1|^3.0
- symfony/options-resolver: ^4.4|^5.0|^6.0|^7.0
Requires (Dev)
- nyholm/psr7: ^1.8.1
- php-cs-fixer/shim: ^v3.40.0
- phpstan/phpstan: ^1.10.46
- phpstan/phpstan-phpunit: ^1.3.15
- symfony/http-client: ^5.0|^6.2|^7.0
- symfony/phpunit-bridge: ^5.3|^6.2|^7.0
- v2.14.0
- v2.13.0
- v2.12.0
- v2.11.0
- v2.10.0
- v2.9.0
- v2.8.0
- v2.7.0
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.1
- v2.1.0
- dev-master / 2.0.x-dev
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.14.1
- v1.14.0
- v1.13.0
- 1.12.0
- 1.11.0
- 1.10.0
- 1.9.0
- 1.8.0
- 1.7.0
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.0
- 1.0.x-dev
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v1.0.0-rc2
- v1.0.0-rc1
- v0.2.0
- 0.1.x-dev
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-bump
This package is auto-updated.
Last update: 2024-09-01 15:54:07 UTC
README
安装
注意:所有示例均针对 v2,如果您使用的是 PHP <7.3,请阅读 v1 文档。
此包需要一个 PSR-18 HTTP 客户端和 PSR-17 HTTP 工厂才能运行。您可以从 psr/http-client-implementation 和 psr/http-factory-implementation 中选择任何一个。
使用 Symfony HttpClient 和 nyholm/psr7 http 工厂的示例,使用 Composer 安装。
composer require symfony/http-client nyholm/psr7 norkunas/onesignal-php-api
现在配置 OneSignal API 客户端
<?php declare(strict_types=1); use OneSignal\Config; use OneSignal\OneSignal; use Symfony\Component\HttpClient\Psr18Client; use Nyholm\Psr7\Factory\Psr17Factory; require __DIR__ . '/vendor/autoload.php'; $config = new Config('your_application_id', 'your_application_auth_key', 'your_auth_key'); $httpClient = new Psr18Client(); $requestFactory = $streamFactory = new Psr17Factory(); $oneSignal = new OneSignal($config, $httpClient, $requestFactory, $streamFactory);
如何使用此库
应用程序 API
查看所有当前 OneSignal 应用程序的详细信息(官方文档)
$myApps = $oneSignal->apps()->getAll();
查看单个 OneSignal 应用程序的详细信息(官方文档)
$myApp = $oneSignal->apps()->getOne('application_id');
创建新的 OneSignal 应用(官方文档)
$newApp = $oneSignal->apps()->add([ 'name' => 'app name', 'gcm_key' => 'key' ]);
更新 OneSignal 应用的名称或配置设置(官方文档)
$oneSignal->apps()->update('application_id', [ 'name' => 'new app name' ]);
创建段(官方文档)
$oneSignal->apps()->createSegment('application_id', [ 'name' => 'Segment Name', 'filters' => [ ['field' => 'session_count', 'relation' => '>', 'value' => 1], ['operator' => 'AND'], ['field' => 'tag', 'relation' => '!=', 'key' => 'tag_key', 'value' => '1'], ['operator' => 'OR'], ['field' => 'last_session', 'relation' => '<', 'value' => '30,'], ], ]);
删除段(官方文档)
$oneSignal->apps()->deleteSegment('application_id', 'segment_id');
查看与您的应用关联的所有结果的详细信息(官方文档)
use OneSignal\Apps; use OneSignal\Devices; $outcomes = $oneSignal->apps()->outcomes('application_id', [ 'outcome_names' => [ 'os__session_duration.count', 'os__click.count', 'Sales, Purchase.sum', ], 'outcome_time_range' => Apps::OUTCOME_TIME_RANGE_MONTH, 'outcome_platforms' => [Devices::IOS, Devices::ANDROID], 'outcome_attribution' => Apps::OUTCOME_ATTRIBUTION_DIRECT, ]);
设备 API
查看您 OneSignal 应用中的一个设备的多项详细信息(官方文档)
$devices = $oneSignal->devices()->getAll();
查看您配置的 OneSignal 应用中现有设备的详细信息(官方文档)
$device = $oneSignal->devices()->getOne('device_id');
将新设备注册到您配置的 OneSignal 应用(官方文档)
use OneSignal\Devices; $newDevice = $oneSignal->devices()->add([ 'device_type' => Devices::ANDROID, 'identifier' => 'abcdefghijklmn', ]);
更新您配置的 OneSignal 应用中现有设备的详细信息(官方文档)
$oneSignal->devices()->update('device_id', [ 'session_count' => 2, 'ip' => '127.0.0.1', // Optional. New IP Address of your device ]);
使用外部用户 ID 更新您 OneSignal 应用中的一个设备标签(官方文档)
$externalUserId = '12345'; $response = $oneSignal->devices()->editTags($externalUserId, [ 'tags' => [ 'a' => '1', 'foo' => '', ], ]);
通知 API
查看多个通知的详细信息(官方文档)
$notifications = $oneSignal->notifications()->getAll();
获取单个通知的详细信息(官方文档)
$notification = $oneSignal->notifications()->getOne('notification_id');
创建并发送通知或电子邮件到段或单个用户。您可以使用此方法以三种方式之一定位用户:通过段、通过筛选或通过设备(至少必须指定一个定位参数)(官方文档)
$oneSignal->notifications()->add([ 'contents' => [ 'en' => 'Notification message' ], 'included_segments' => ['All'], 'data' => ['foo' => 'bar'], 'isChrome' => true, 'send_after' => new \DateTime('1 hour'), 'filters' => [ [ 'field' => 'tag', 'key' => 'is_vip', 'relation' => '!=', 'value' => 'true', ], [ 'operator' => 'OR', ], [ 'field' => 'tag', 'key' => 'is_admin', 'relation' => '=', 'value' => 'true', ], ], // ..other options ]);
将通知标记为已打开(官方文档)
$oneSignal->notifications()->open('notification_id');
停止计划中的或当前正在发送的通知(《官方文档》)
$oneSignal->notifications()->cancel('notification_id');
通知历史(《官方文档》)
$oneSignal->notifications()->history('notification_id', [ 'events' => 'clicked', // or 'sent' 'email' => '[email protected]', ]);
有疑问?
如果您有任何疑问,请发起讨论。
许可
此库采用MIT许可证发布。有关详细信息,请参阅附带的LICENSE文件。