dutchie027/apnpush

用于Apple推送通知服务(APNs)的PHP客户端

0.30.0 2022-04-10 15:14 UTC

README

Software License

Apnpush是一个简单的PHP库,用于通过Apple的APN服务发送推送通知。

特性

  • 使用新的Apple APNs HTTP/2连接
  • 支持基于JWT的认证
  • 支持基于证书的认证
  • 支持新的iOS功能,如Collapse IDs、Subtitles和可变通知
  • 使用并发请求到APNs
  • 在APNs生产环境中测试并运行正常

要求

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

安装

通过Composer

$ composer require dutchie027/apnpush

入门指南

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

use Apnpush\Client;
use Apnpush\Notification;
use Apnpush\Payload;
use Apnpush\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 = Apnpush\AuthProvider\Token::create($options);

$alert = Alert::create()->setTitle('Hello!')->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);
}

$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

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

测试

# run php fixer
$ composer fix

# run phpstan
$ compser stan

# run phpunit tests
$ composer test

# run all three above test in sequence
$ composer runall

待办事项

  • 修复测试
    • 确保它们都能正确移植/移动(pslam)
    • 检查是否已安装http/2
  • 清理文档
  • 其他事项

行为准则

该项目遵循行为准则。通过参与该项目及其社区,您应遵守此准则。

许可

Apnpush在MIT许可下发布。有关详细信息,请参阅LICENSE

版本控制

此代码使用Semver。这意味着版本用MAJOR.MINOR.PATCH标记。只有新的主要版本才会允许打破向后兼容性(BC)。

标记为@experimental@internal的类不包括在我们的向后兼容性承诺中。您也不能保证方法返回的值总是相同的。您可以保证数据类型不会改变。

贡献

欢迎贡献!要贡献,请熟悉CONTRIBUTING.md

致谢