PHP framework for easy interaction with the Apple Push Notification Service.
You can use composer to include this framework into your project. The project is available through Packagist.
composer require zandor300/apnsframework
This framework depends on the firebase/php-jwt package.
use APNSFramework;
$teamId = "";
$bundleId = "";
$authKeyPath = "";
$authKeyId = "";
try {
$apns = new APNS($teamId, $bundleId, $authKeyPath, $authKeyId);
} catch (APNSException $e) {
echo "Error: " . $e->getMessage();
// Handle exception
}
$notification = new APNSNotification();
$notification->setTitle("Example Notification");
$notification->setBody("This is an example notification!");
$notification->setBadge(2);
try {
$tokenString = "<FILL IN THE APNS TOKEN>";
$environment = APNSTokenEnvironment::development;
//$environment = APNSTokenEnvironment::production;
$token = new APNSToken($tokenString, $environment);
} catch (APNSException $e) {
echo "Error: " . $e->getMessage();
// Handle exception
}
try {
$apns->sendNotification($notification, $token);
} catch (APNSDeviceTokenInactive $e) {
// Remove device token from database since it's inactive.
} catch (APNSException $e) {
echo "Error: " . $e->getMessage();
// Handle exception
}
You might need to set the root certificate for the APNS request using setRootCertificatePath
on the APNS
object.
You can download this certificate using the link in the Apple Developer documentation:
Establish a Trusted Connection to APNs
> AAA Certificate Services root certificate
$apns->setRootCertificatePath(__DIR__ . "/AAACertificateServices.crt");