edamov / pushok
PHP 客户端用于 Apple Push Notification Service (APNs) - 使用基于 token 的 (JWT 与 p8 私钥) 或基于证书的认证,通过新的 APNs HTTP/2 协议发送推送通知到 iOS
0.16.0
2024-02-26 10:34 UTC
Requires
- php: ^8.1
- ext-curl: *
- ext-intl: *
- ext-json: *
- ext-openssl: *
- ext-xml: *
- lib-curl: >=7.46.0
- lib-openssl: >=1.0.2.5
- web-token/jwt-library: ^3.0
Requires (Dev)
- ext-xdebug: *
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.4
- 0.16.0
- 0.15.6
- 0.15.5
- 0.15.4
- 0.15.3
- 0.15.2
- 0.15.1
- 0.15.0
- 0.14.3
- 0.14.2
- 0.14.1
- 0.14.0
- 0.13.0
- 0.12.1
- 0.12.0
- 0.11.4
- 0.11.3
- 0.11.2
- 0.11.1
- 0.11.0
- 0.10.5
- 0.10.4
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.0
- 0.8.0
- 0.7.0
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.1
- 0.3.0
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- dev-master / 0.1.x-dev
- 0.1.0
- dev-php-8.0
- dev-feature/support-ukraine
- dev-feature/update-jwt-libraries
This package is auto-updated.
Last update: 2024-08-26 11:41:02 UTC
README
你喜欢这个库吗?请考虑 捐赠 以支持乌克兰 🇺🇦
Pushok 是一个简单的 PHP 库,用于向 APNs 发送推送通知。
特性
- 使用新的 Apple APNs HTTP/2 连接
- 支持基于 JWT 的认证
- 支持基于证书的认证
- 支持新的 iOS 10 功能,如 Collapse IDs、Subtitles 和 Mutable Notifications
- 使用并发请求到 APNs
- 已在 APNs 生产环境中测试并正常工作
要求
- PHP >= 8.1
- lib-curl >= 7.46.0 (启用 http/2 支持)
- lib-openssl >= 1.0.2e
满足要求的 Docker 镜像可以在此找到 此处。或者,您可以按照 这篇教程 创建自己的带有 HTTP/2 支持的 curl Docker 镜像。
安装
通过 Composer
$ composer require edamov/pushok
入门指南
使用 JWT 令牌。有关更多信息,请参阅 处理来自 APNs 的通知响应。
<?php require __DIR__ . '/vendor/autoload.php'; use Pushok\AuthProvider; use Pushok\Client; use Pushok\Notification; use Pushok\Payload; use Pushok\Payload\Alert; $options = [ 'key_id' => 'AAAABBBBCC', // The Key ID obtained from Apple developer account 'team_id' => 'DDDDEEEEFF', // The Team ID obtained from Apple developer account 'app_bundle_id' => 'com.app.Test', // The bundle ID for app obtained from Apple developer account 'private_key_path' => __DIR__ . '/private_key.p8', // Path to private key 'private_key_secret' => null // Private key secret ]; // Be aware of thing that Token will stale after one hour, so you should generate it again. // Can be useful when trying to send pushes during long-running tasks $authProvider = AuthProvider\Token::create($options); $alert = Alert::create()->setTitle('Hello!'); $alert = $alert->setBody('First push notification'); $payload = Payload::create()->setAlert($alert); //set notification sound to default $payload->setSound('default'); //add custom value to your notification, needs to be customized $payload->setCustomValue('key', 'value'); $deviceTokens = ['<device_token_1>', '<device_token_2>', '<device_token_3>']; $notifications = []; foreach ($deviceTokens as $deviceToken) { $notifications[] = new Notification($payload,$deviceToken); } // If you have issues with ssl-verification, you can temporarily disable it. Please see attached note. // Disable ssl verification // $client = new Client($authProvider, $production = false, [CURLOPT_SSL_VERIFYPEER=>false] ); $client = new Client($authProvider, $production = false); $client->addNotifications($notifications); $responses = $client->push(); // returns an array of ApnsResponseInterface (one Response per Notification) foreach ($responses as $response) { // The device token $response->getDeviceToken(); // A canonical UUID that is the unique ID for the notification. E.g. 123e4567-e89b-12d3-a456-4266554400a0 $response->getApnsId(); // Status code. E.g. 200 (Success), 410 (The device token is no longer active for the topic.) $response->getStatusCode(); // E.g. The device token is no longer active for the topic. $response->getReasonPhrase(); // E.g. Unregistered $response->getErrorReason(); // E.g. The device token is inactive for the specified topic. $response->getErrorDescription(); $response->get410Timestamp(); }
使用证书 (.pem)。与 JWT 代码(上方)的不同之处仅在于初始化。请记住,自己包含其余代码。
<?php $options = [ 'app_bundle_id' => 'com.app.Test', // The bundle ID for app obtained from Apple developer account 'certificate_path' => __DIR__ . '/private_key.pem', // Path to private key 'certificate_secret' => null // Private key secret ]; $authProvider = AuthProvider\Certificate::create($options); ...
注意:请参阅 此帖子 了解 ssl 验证。
选项以进行修改。请参阅 向 APNs 发送通知请求。
<?php $client = new Client($authProvider, $production = false); $client->addNotifications($notifications); // Set the number of concurrent requests sent through the multiplexed connections. Default : 20 $client->setNbConcurrentRequests( 40 ); // Set the number of maximum concurrent connections established to the APNS servers. Default : 1 $client->setMaxConcurrentConnections( 5 ); $responses = $client->push();
测试
$ composer test
安全性
如果您发现任何安全问题,请通过电子邮件 edamov@gmail.com 反馈,而不是使用问题跟踪器。
致谢
许可协议
MIT 许可协议 (MIT)。请参阅 许可文件 获取更多信息。