PHP code example of davibr / apn-push
1. Go to this page and download the library: Download davibr/apn-push 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/ */
davibr / apn-push example snippets
use Apple\ApnPush\Certificate\Certificate;
use Apple\ApnPush\Protocol\Http\Authenticator\CertificateAuthenticator;
use Apple\ApnPush\Sender\Builder\Http20Builder;
use Apple\ApnPush\Model\Alert;
use Apple\ApnPush\Model\Aps;
use Apple\ApnPush\Model\Payload;
use Apple\ApnPush\Model\Notification;
use Apple\ApnPush\Model\DeviceToken;
use Apple\ApnPush\Model\Receiver;
use Apple\ApnPush\Exception\SendNotification\SendNotificationException;
// Create certificate and authenticator
$certificate = new Certificate(__DIR__.'/cert.pem', '');
$authenticator = new CertificateAuthenticator($certificate);
// Build sender
$builder = new Http20Builder($authenticator);
$builder->addDefaultVisitors();
$sender = $builder->build();
// Create payload
$alert = new Alert('hello ;)');
$aps = new Aps($alert);
$payload = new Payload($aps);
// Create notification
$notification = new Notification($payload);
$receiver = new Receiver(
new DeviceToken('6b4d687c1292f1ff05b5653951be4e5f838ce6d39d6b1be1801fe8dcc35713c9'),
'you.app.id'
);
try {
$sender->send($receiver, $notification);
} catch (SendNotificationException $e) {
print 'Fail send message: '.$e->getMessage()."\n";
}