exeerik/pushok

针对 Apple 推送通知服务(APNs)的定制 PHP 客户端 - 使用基于令牌(JWT 与 p8 私钥)或基于证书的身份验证,通过新的 APNs HTTP/2 协议发送 iOS 推送通知

0.11.1 2020-04-02 16:46 UTC

README

PHP >= 7.2 Latest Version on Packagist Total Downloads Software License

Pushok 是一个简单的 PHP 库,用于向 APNs 发送推送通知。

功能

  • 使用新的 Apple APNs HTTP/2 连接
  • 支持基于 JWT 的身份验证
  • 支持基于证书的身份验证
  • 支持新的 iOS 10 功能,如折叠 ID、副标题和可变通知
  • 使用并发请求到 APNs
  • 已在 APNs 生产环境中测试和运行
  • 关键警报功能

要求

  • PHP >= 7.2
  • lib-curl >= 7.46.0(启用 http/2 支持)
  • lib-openssl >= 1.0.2e

安装

通过 Composer

$ composer require edamov/pushok

入门

<?php
require __DIR__ . '/vendor/autoload.php';

use Pushok\AuthProvider;
use Pushok\Client;
use Pushok\Notification;
use Pushok\Payload;
use Pushok\Payload\Alert;
use Pushok\Payload\Sound;

$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
];

$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');

//if you want to send a critical alert you need to specify the following settings
//and remove previous set "$payload->setSound('default');"
$sound = Sound::create()->setCriticalSoundEnabled(1);
$sound = $sound->setCriticalSoundName('default');
$sound = $sound->setCriticalSoundVolume(1.0);
$payload->setSound($sound); 

//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);
}

$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) {
    $response->getApnsId();
    $response->getStatusCode();
    $response->getReasonPhrase();
    $response->getErrorReason();
    $response->getErrorDescription();
}

测试

$ composer test

安全

如果您发现任何安全相关的问题,请通过电子邮件 erik@exeerik.de 联系我们,而不是使用问题跟踪器。

鸣谢

许可

MIT 许可证(MIT)。有关更多信息,请参阅许可文件