Using ApnsPHP library with CodeIgniter to send Push Notifications to iOS devices

1.00 avg. rating (54% score) - 1 vote

I recently needed to use PHP to send Push Notification to iOS and Android devices. While sending push notification to an Android device is straight forward once the device registration ID is known (just a few cURL calls to POST to Google API server), it was a mess trying to do the same for iOS devices.

First, Apple likes to make things complicated for developers and the sending of push notification to iOS devices requires the use of a PEM key, or two keys to be exact (one for debug and one for release), in order to communicate with the server. To implement this communication protocol, the usage of a third party library such as ApnsPHP is required and the code to send a push message is as follows:


// Using Autoload all classes are loaded on-demand
require_once 'ApnsPHP/Autoload.php';

$push = new ApnsPHP_Push(
ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
'server_cerificates_bundle_sandbox.pem'
);
$push->setRootCertificationAuthority('entrust_root_certification_authority.pem');
$push->connect();
$message = new ApnsPHP_Message('1e82db91c7ceddd72bf33d74ae052ac9c84a065b35148ac401388843106a7485');
$message->setCustomIdentifier("Message-Badge-3");
$message->setBadge(3);
$message->setText('Hello APNs-enabled device!');
$message->setSound();
$message->setCustomProperty('acme2', array('bang', 'whiz'));
$message->setCustomProperty('acme3', array('bing', 'bong'));
$message->setExpiry(30);
$push->add($message);
$push->send();
$push->disconnect();
 

However, while I got the code working on first try, subsequently I encountered a problem while trying to use the code from a CodeIgniter controller:

Fatal error: Uncaught exception ‘Exception’ with message ‘Class file ‘/wwwdata/www/html/application/controllers/CI/DB/mysql/result.php’ does not exists’ in /wwwdata/www/html/application/controllers/ApnsPHP/Autoload.php:49 

A Google search of the error message found only a few results. Among them, this stackoverflow post proposed that the Autoload feature of PHP, used in the APNS library to avoid the need of manually declaring require_once every time a new PHP class is needed, is conflicting with CodeIgnter, which perhaps is also trying to use the same Autoload feature to initialize its MVC model. Another post here described the same problem but provided no solution. 

After some consideration I decided to try the hard way – which is to remove the use of Autoload and replace it which manual require_once declarations. With this modification, the require_once declaration above is changed to the following long list:

require_once 'ApnsPHP/Abstract.php';
require_once 'ApnsPHP/Exception.php';
require_once 'ApnsPHP/Feedback.php';
require_once 'ApnsPHP/Message.php';
require_once 'ApnsPHP/Log/Embedded.php';
require_once 'ApnsPHP/Log/Interface.php';
require_once 'ApnsPHP/Message/Custom.php';
require_once 'ApnsPHP/Message/Exception.php';
require_once 'ApnsPHP/Push/Exception.php';
require_once 'ApnsPHP/Push/Server.php';
require_once 'ApnsPHP/Push/Server/Exception.php';

Surprisingly, only with some extra manual modifications of a few classes in the ApnsPHP library to include the necessary classes, the library works and is able to send push notification from CodeIgniter. For example, the following line must be added at the top of server.php, since it refers to Push.php:

require_once realpath(dirname(__FILE__).'/..').'/Push.php';

The complete library with the above modification can be downloaded here. As I said, this is not an elegant solution due to too many manual modifications required. Feel free to comment if you have any ideas for a better approach which does not involve extensive modification of the library.



1.00 avg. rating (54% score) - 1 vote
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

One thought on “Using ApnsPHP library with CodeIgniter to send Push Notifications to iOS devices

  • May 6, 2015 at 4:37 pm
    Permalink

    I really like and appreciate your blog post.Thanks Again. ckcdbkedfkkeaced

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>