zandor300/apnsframework

PHP framework for easy interaction with Apple's Push Notification Service.

1.6.1 2024-07-29 10:07 UTC

README

Build Version License PHP Version Downloads

PHP framework for easy interaction with the Apple Push Notification Service.

Install

You can use composer to include this framework into your project. The project is available through Packagist.

composer require zandor300/apnsframework

Dependencies

This framework depends on the firebase/php-jwt package.

Usage

Creating APNS object.

use APNSFramework;

$teamId = "";
$bundleId = "";
$authKeyPath = "";
$authKeyId = "";

try {
    $apns = new APNS($teamId, $bundleId, $authKeyPath, $authKeyId);
} catch (APNSException $e) {
    echo "Error: " . $e->getMessage();
    // Handle exception
}

Creating a basic notification

$notification = new APNSNotification();

$notification->setTitle("Example Notification");
$notification->setBody("This is an example notification!");

$notification->setBadge(2);

Creating a token object

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
}

Sending the notification

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
}

Troubleshooting

Certificate errors

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