PHP code example of gepo / apns-http2

1. Go to this page and download the library: Download gepo/apns-http2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gepo / apns-http2 example snippets




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



nt = 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);



pns\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;
    }
}