swaroop / apns2
简单的、富有表达力的PHP HTTP2 API,用于Apple推送通知服务(APNs),包含全面的文档/常量。
2.0.2
2022-05-31 06:27 UTC
Requires
- php: >=5.4.0
README
简单的、富有表达力的PHP HTTP2 API,用于Apple推送通知服务(APNs),包含全面的文档/常量。
指南
- 确保CURL支持HTTP/2(对于php版本 < 5.5.24,请查看这里)
- 创建通用的推送通知客户端SSL证书 (.p12)
- 将 .p12 转换为 .pem
openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
- 进入
composer require swaroop/apns2
用法
使用类
$connection = new Swaroop\Apns2\Connection(); $connection->sandbox = false; $connection->certPath = '/path/to/http2/cert.pem'; $aps = new Swaroop\Apns2\MessageAPSBody(); $aps->alert = 'test 1'; $aps->sound = 'default'; $message = new Swaroop\Apns2\Message(); $message->aps = $aps; $options = new Swaroop\Apns2\Options(); $options->apnsTopic = 'your.bundle.id'; $responses = $connection->send([ '81fbf7e296f6c94755832a48476182e4e9586a380116e18a46531b62349504f0', 'e2d0b464813b6b2371d745dff2b1e5fb6b83b07f7dcd98cc9f1346a7752dcc45', ], $message, $options); $connection->close(); var_dump($responses);
使用数组
$connection = new \Swaroop\Apns2\Connection(['sandbox' => true, 'cert-path' => '/path/to/http2/cert.pem']); $responses = $connection->send([ '81fbf7e296f6c94755832a48476182e4e9586a380116e18a46531b62349504f1' // invalid ], [ 'aps' => [ 'alert' => 'test 2', 'sound' => 'default', ] ], [ 'apns-topic' => 'your.bundle.id', ]); $connection->close();
检查响应数据
echo "check response: {$responses[0]->apnsId} == ${uuid}\n"; assert($responses[0]->apnsId == $uuid); $reason = \Swaroop\Apns2\Response::REASON_BAD_DEVICE_TOKEN; echo "check response: {$responses[0]->reason} == ${reason}\n"; assert($responses[0]->reason == $reason);
许可证
MIT