gepo/apns-http2
Composer 安装命令:
composer require gepo/apns-http2
包简介
Library for sending APNS over HTTP/2.
README 文档
README
PHP library for sending notifications via Apple Push Notification service (APNS) over HTTP/2.
Installation
composer require gepo/apns-http2
Requirements
AAA Certificate
Since 29 March 2021, the AAA certificate is required. You can read about it and get it here: https://developer.apple.com/news/?id=7gx0a2lp.
On Ubuntu, you must put it in /usr/local/share/ca-certificates/extra/ (create the path if it doesn't exist), and run update-ca-certificates as root).
cURL HTTP/2 support
You need cURL with HTTP/2 support installed on your system for this library to work. cURL supports HTTP/2 starting version 7.43.0.
Check if your installation supports it
Here's a simple script to check if your installation supports cURL with HTTP/2:
<?php defined('CURL_VERSION_HTTP2') || define('CURL_VERSION_HTTP2', 65536); $version = curl_version(); if (($version['features'] & CURL_VERSION_HTTP2) !== 0) { echo 'HTTP/2 supported'.PHP_EOL; } else { echo 'HTTP/2 not supported'.PHP_EOL; }
Alternatively, you may check the cURL version in command line to make sure it's greater or equal to 7.43.0:
curl --version
Installation on Ubuntu
#!/bin/bash # Update version to latest, found here: https://curl.se/download/ VERSION=7.76.1 cd ~ sudo apt-get update -y sudo apt-get install -y build-essential nghttp2 libnghttp2-dev libssl-dev wget wget https://curl.haxx.se/download/curl-${VERSION}.tar.gz tar -xzvf curl-${VERSION}.tar.gz && rm -f curl-${VERSION}.tar.gz && cd curl-${VERSION} ./configure --prefix=/usr/local --with-ssl --with-nghttp2 make -j4 sudo make install sudo ldconfig cd ~ && rm -rf curl-${VERSION}
This script is based on https://gist.github.com/jjpeleato/3327c2e38fc0fea7d6602401f9849809
Installation on MacOS X
To install it on OS X with Homebrew (the example is for PHP 5.6):
brew install curl --with-nghttp2 --with-openssl brew link curl --force brew reinstall php56 --with-homebrew-curl
Usage
Simple
<?php require_once __DIR__ . '/vendor/autoload.php'; $client = new \Apns\Client(__DIR__ . '/certs/apns-dev-cert.pem', true); // true is for sandbox $message = (new \Apns\Message()) ->setDeviceIdentifier('a00915e74d60d71ba3fb80252a5e197b60f2e7743f61b4411c713e9aabd2854f') ->setAlert('Test message') ->setTopic('com.mycompany.myapp') ; $client->send($message);
Complex
<?php require_once __DIR__ . '/vendor/autoload.php'; use Apns\Client as ApnsClient; use Apns\Message as ApnsMessage; use Apns\Exception\ApnsException; $token = 'a00915e74d60d71ba3fb80252a5e197b60f2e7743f61b4411c713e9aabd2854f'; // Check if token format is valid if ((!ctype_xdigit($token)) || (64 != strlen($token))) { die('Token is invalid!'); } // Create client with production certificate and passphrase $client = new ApnsClient( [ __DIR__ . '/certs/apns-prod-cert.pem', 'my-passphrase' ], false // false is for production ); // Get topic from certificate file if ($cert = openssl_x509_parse(file_get_contents($apnsClient->getSslCert()[0]))) { $topic = $cert['subject']['UID']; } // Create message $message = (new ApnsMessage()) ->setDeviceIdentifier($token) ->setAlert('This is a test message sent on '.gmdate('r')) ->setData([ 'Key1' => 'Value1', 'Key2' => 'Value2', 'Key3' => 'Value3', ]) ->setTopic($topic); // Send it and catch errors try { $client->send($message); } catch (ApnsException $e) { // https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW17 switch ($e->getMessage()) { case 'BadDeviceToken': case 'ExpiredProviderToken': case 'InvalidProviderToken': case 'MissingProviderToken': case 'Unregistered': // do something, ie. remove the token from your list break; case 'BadCollapseId': case 'BadExpirationDate': case 'BadMessageId': case 'BadPriority': // do something, ie. check your parameters break; case 'BadTopic': case 'MissingTopic': case 'DeviceTokenNotForTopic': // do something, ie. check that your topic is ok break; case 'BadCertificate': // do something, ie. check the certificate you provided break; case 'BadCertificateEnvironment': // do something, ie. check your certificate/environment (sandbox or production) break; case 'TooManyRequests': case 'TooManyProviderTokenUpdates': // do something, ie. throttle your requests break; default: // do something break; } }
gepo/apns-http2 适用场景与选型建议
gepo/apns-http2 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 122.45k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2016 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 gepo/apns-http2 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gepo/apns-http2 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 122.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 16
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-15