PHP code example of autoxloo / apns

1. Go to this page and download the library: Download autoxloo/apns 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/ */

    

autoxloo / apns example snippets


composer 

use autoxloo\apns\AppleNotificationServer;

$appleCertPath = __DIR__ . '/wxv_cert.pem';

$apns = new AppleNotificationServer(
    $appleCertPath,
    $apiUrl = 'https://api.push.apple.com/3/device',
    $apiUrlDev = 'https://api.sandbox.push.apple.com/3/device',
    $apnsPort = 443,
    $pushTimeOut = 10,
    $topic = null,
    $expiration = null,
    $pushType = null
);

use autoxloo\apns\AppleNotificationServer;

$appleCertPath = __DIR__ . '/wxv_cert.pem';
$token = 'some device token';
$payload = [
    'some key1' => 'some value1',
    'some key2' => 'some value2',
];

$apns = new AppleNotificationServer($appleCertPath);
$response = $apns->send($token, $payload);

use autoxloo\apns\AppleNotificationServer;

$appleCertPath = __DIR__ . '/wxv_cert.pem';
$tokens = [
    'some device token',
    'some other device token',
];
$payload = [
    'some key1' => 'some value1',
    'some key2' => 'some value2',
];

$apns = new AppleNotificationServer($appleCertPath);
$response = $apns->sendToMany($tokens, $payload);

use autoxloo\apns\AppleNotificationServer;

$appleCertPath = __DIR__ . '/wxv_cert.pem';
$token = 'some device token';
$payload = [
    'some key1' => 'some value1',
    'some key2' => 'some value2',
];

$apns = new AppleNotificationServer($appleCertPath);
$apns->setPushType(AppleNotificationServer::PUSH_TYPE_BACKGROUND);  // sets `apns-push-type` header.
// other available set methods:
$apns->setTopic('some topic');  // sets `apns-topic` header.
$apns->setExpiration(time() + 30);  // sets `apns-expiration` header.
$apns->setExpiration(0);  // sets `apns-expiration` header. If the value is 0, APNs attempts to deliver
                          // the notification only once and doesn’t store it.
$response = $apns->send($token, $payload);

use autoxloo\apns\AppleNotificationServer;

$apns = new AppleNotificationServer($appleCertPath, 'https://api.sandbox.apple.com/3/device', false);

php composer.phar