Push notifications to iOS application via PHP

11 Mar 2016 in PHP, iOS

This is a skeleton on how to send push notifications to iOS devices from PHP with the help of ApnsPHP library:

// Get all device ids uninstalled the application
$feedback = new ApnsPHP_Feedback(
$deviceTokens = $feedback->receive();
foreach ($deviceTokens as $deviceId) {
    // Remove $deviceId from our database

// Now construct a list of active device ids
$devices = [];

// Get them from our database of other data provider
// ...

// Instantiate a new ApnsPHP_Push object
$push = new ApnsPHP_Push(

// Set the Provider Certificate passphrase

// Set the Root Certificate Autority to verify the Apple remote peer

// Connect to the Apple Push Notification Service

foreach ($devices as $deviceId) {
    // Instantiate a new Message with a single recipient
    $message = new ApnsPHP_Message($deviceId);

    // Set a custom identifier
    // To get back this identifier use the getCustomIdentifier() method
    // over a ApnsPHP_Message object retrieved with the getErrors() message.

    // Set badge icon to "3"

    // Set a simple welcome text
    $message->setText('simple welcome text');

    // Play the default sound

    // Set a custom property
    $message->setCustomProperty('acme2', array('bang', 'whiz'));

    // Set another custom property
    $message->setCustomProperty('acme3', array('bing', 'bong'));

    // Set the expiry value to 30 seconds

    // Add the message to the message queue

// Send all messages in the message queue

// Disconnect from the Apple Push Notification Service

// Examine the error message container
$errors = $push->getErrors();

Also, at the library's github page, there are instructions on how to generate the push certificate and a demo iOS application.