Use PHP to send APNS iOS and GCM Android push notifications


Through our own internal projects it has become a requirement in the past to implement a system that integrates with a mobile application. Specifically we have implemented systems that send out push notifications to iOS and Android based devices.

The conditions through which we would be sending the notifications are somewhat irrelevant, though it is important to indicate in this post that we are parsing a database of device IDs and tokens in order to build an array of devices to submit to the function that actually handles the push notifications.

This means that the workflow for this type of integration would involve someone downloading an application on their iOS or Android device that requests their permission to send push notifications. Once permissions are given, the application will send the device key to our systems which will then record it in a database for further use.

For APNS (Apple push notification service), you are required to register a certificate key that is associated with your application. This means that after permissions for push notifications is given, you are only able to authenticate messages to those authorized devices for your particular app. There is already a lot of documentation on how to create, install and integrate a development and production certificate for APNS.

For GCM (Google Cloud Messaging), you have to register with cloud messaging which will generate an API key that you will use to send messages. Much simpler than APNS in my opinion.

MySQL Database to store device info

I dont really need to spend much time on this, other than to highlight the schema we’re using in this particular scenario to better help you understand the functions for APNS and GCM notifications in PHP.

We use a mysql database to store device keys and uuid’s as registered and transmitted by the mobile app :

“dtoken” is what we use to build an array of devices to notify. “dtype will store either the “iOS” or “Android” device types. This is how we split the notification to the proper category.

APNS Notification function in PHP

To build a function to send APNS, we used the APNSPHP implementation. There’s a lot of documentation and examples at the github page for this project that you may find helpful in your own custom implementation.

We took the example in this project and modified it to integrate as a standalone function to send push notifications to iOS devices :

Pretty straightforward, right? The function is called with the actual push message as a variable. Then we select applicable devices to send the message through and put it in an array, then apply the array to the push message variable and send.

GCM notifications in PHP

For GCM, we build a similar function but we’re using curl to connect to google’s API and send the message. You can see the original php example on Github that we used to build this function, or see the example below :

During your testing, you could set your php application so you can access these functions directly and var_dump the various output points in order to see whats happening, and in particular the debug response message that Google or Apple will send once the message has been sent.

I hope this helps you build your APNS or GCM notification systems! You dont always have to subscribe to 3rd party services unless you are looking at high volume notifications. Its very straightforward for an intermediate developer to implement their own systems free of charge! 🙂