sasa-b / apns2

APNS - 苹果推送通知服务 PHP 客户端

3.0.0 2024-01-31 15:26 UTC

This package is auto-updated.

Last update: 2024-09-30 01:16:25 UTC


README

APNS - 苹果推送通知服务 PHP 客户端

PHP >= 7.0

要求

PHP >= 7.0
lib-curl >= 7.46.0 (with http/2 support enabled)
lib-openssl >= 1.0.2e

如果使用 PHP 7.0,则需要从源码编译 PHP,并使用支持 HTTP2 请求的新版 lib-curl

./configure --with-curl=/usr/local/include/curl --with-libdir=lib64 --with-openssl ...

./make

安装

composer require sasa-b/apns2

入门

证书提供者信任

require '/vendor/autoload.php';

use SasaB\Apns\Client;
use SasaB\Apns\Provider\Certificate;

use SasaB\Apns\Payload\Aps;
use SasaB\Apns\Payload\Alert;
use SasaB\Apns\Notification;

$certificate = Certificate::fromFile('/PushCert.pem');

$client = Client::auth($certificate);

$notification = new Notification("{device_token}");

$notification->setAps(new Aps(new Alert('Hello World')));

$response = $client->send($notification);

echo (string) $response->getApnsId()."\n";
echo $response;

令牌密钥提供者信任

require '/vendor/autoload.php';

use SasaB\Apns\Client;
use SasaB\Apns\Provider\JWT;
use SasaB\Apns\Provider\TokenKey;

use SasaB\Apns\Payload\Aps;
use SasaB\Apns\Payload\Alert;
use SasaB\Apns\Notification;

$tokenKey = new TokenKey('{token_key}');
$tokenKey->loadFromFile('/AuthKey.p8');

$jwt = JWT::new('{team_id}', $tokenKey);

if ($jwt->hasExpired()) {
    $jwt->refresh($tokenKey);
}

$client = Client::auth($jwt);

$notification = new Notification("{device_token}");

$notification->setPushTopic('com.vendor.app');

$notification->setAps(new Aps(new Alert('Hello World')));

$response = $client->send($notification);

echo (string) $response->getApnsId()."\n";
echo $response;