myqaa/apns-http2

用于通过HTTP/2发送APNS的库。

dev-master 2022-08-10 14:41 UTC

This package is auto-updated.

Last update: 2024-09-10 18:51:01 UTC


README

Build Status Dependency Status Scrutinizer Code Quality Code Coverage License

PHP库,用于通过HTTP/2发送Apple推送通知服务(APNS)的推送通知。

安装

composer require gepo/apns-http2

要求

AAA证书

自2021年3月29日起,需要AAA证书。您可以在这里了解它并获取它:https://developer.apple.com/news/?id=7gx0a2lp

在Ubuntu上的安装

cURL HTTP/2支持

为了让这个库正常工作,您的系统上需要安装具有HTTP/2支持的cURL。从版本7.43.0开始,cURL支持HTTP/2。

检查您的安装是否支持它

这是一个简单的脚本来检查您的安装是否支持带有HTTP/2的cURL

<?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;
}

或者,您也可以在命令行中检查cURL的版本,确保它大于或等于7.43.0

curl --version

在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}

此脚本基于 https://gist.github.com/jjpeleato/3327c2e38fc0fea7d6602401f9849809

在MacOS X上的安装

使用Homebrew在OS X上安装它(以下示例为PHP 5.6)

brew install curl --with-nghttp2 --with-openssl
brew link curl --force
brew reinstall php56 --with-homebrew-curl

用法

简单

<?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);

复杂

<?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;
    }
}