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
$push = new ApnsPHP_Push(
$message = new ApnsPHP_Message('1e82db91c7ceddd72bf33d74ae052ac9c84a065b35148ac401388843106a7485');
$message->setText('Hello APNs-enabled device!');
$message->setCustomProperty('acme2', array('bang', 'whiz'));
$message->setCustomProperty('acme3', array('bing', 'bong'));
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:
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:
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:
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.