zandor300/apnsframework

PHP 框架,用于轻松与苹果推送通知服务交互。

1.6.1 2024-07-29 10:07 UTC

README

Build Version License PHP Version Downloads

PHP 框架,用于轻松与苹果推送通知服务交互。

安装

您可以使用 composer 将此框架包含到您的项目中。该项目通过 Packagist 提供。

composer require zandor300/apnsframework

依赖项

此框架依赖于 firebase/php-jwt 包。

使用方法

创建 APNS 对象。

use APNSFramework;

$teamId = "";
$bundleId = "";
$authKeyPath = "";
$authKeyId = "";

try {
    $apns = new APNS($teamId, $bundleId, $authKeyPath, $authKeyId);
} catch (APNSException $e) {
    echo "Error: " . $e->getMessage();
    // Handle exception
}

创建基本通知

$notification = new APNSNotification();

$notification->setTitle("Example Notification");
$notification->setBody("This is an example notification!");

$notification->setBadge(2);

创建令牌对象

try {
    $tokenString = "<FILL IN THE APNS TOKEN>";

    $environment = APNSTokenEnvironment::development;
    //$environment = APNSTokenEnvironment::production;

    $token = new APNSToken($tokenString, $environment);
} catch (APNSException $e) {
    echo "Error: " . $e->getMessage();
    // Handle exception
}

发送通知

try {
    $apns->sendNotification($notification, $token);
} catch (APNSDeviceTokenInactive $e) {
    // Remove device token from database since it's inactive.
} catch (APNSException $e) {
    echo "Error: " . $e->getMessage();
    // Handle exception
}

故障排除

证书错误

您可能需要使用 setRootCertificatePathAPNS 对象上设置 APNS 请求的根证书。您可以使用苹果开发者文档中的链接下载此证书: Establish a Trusted Connection to APNs > AAA Certificate Services root certificate

$apns->setRootCertificatePath(__DIR__ . "/AAACertificateServices.crt");