PHP XMPP Google Talk send notification message to IPhone

The example below, I am going to show you how you can send notifications using PHP XMPP library. I am using CodeIgniter 2.0, a php framework, PHP XMPP library and MySQL database in the example.

The receiving end, I am using an iPhone installed with an instant messaging app, setup with Google Talk account to receive push notifications. I chose IM+ free version. It’s up to you which app you prefer but I recommend at least one with push notifications on new messages so it served as some sort of “notification”.

What you need?

  • Latest version of PHP XMPP library.
  • 2 x Google talk account, one for receiving and one for sending the notifications.
  • PHP enabled server.
  • Optional – MySql database for storing of notification queues. You may choose to read the notifications queue from a file, memory pipe and etc….

PHP server settings

  • Important! Ensure PHP OpenSSL module is enabled. I encountered infinite loop when connecting to GTalk server while OpenSSL module is disabled. Click here if you need help.

Firstly, install the PHP XMPP package in “/libraries/xmpp”.

Create the model which reads the notification queue for new message to send.

//M_notification_queue.php file
function getUnsendNotifications(){
	//Load the database
	//Build the sql query
	//Retrieve unsent messages only
	//Retrieve from table notification
	$query = $this->db->get('notification');
	//return the active record handler
	return $query;

Implement the sending controller

//notification.php controller file
private $gtalk_username = ""; //fake email
private $gtalk_password = "password"; //fake password
private $check_notification_interval = 10; //frequency to check for unsent message

function sendNotificationCronJob(){
	//load required models
	//initialize the xmpp library
	$conn = new XMPPHP_XMPP(
		'', //google talk xmpp server address
		5222, //port
		$this->gtalk_username , //username
		$this->gtalk_password, //password
		'xmpphp', //client
		'', //domain
		$printlog=true, //echo out the log
		$loglevel=XMPPHP_Log::LEVEL_ERROR //only on error, we echo out
	//keep looping, check for unsent messages and sent
		//pause execution
		//retrieve unsent messages
		$messageQuery = $this->M_notification->getUnsendNotifications();
		//check if there is unsent message
		if($messageQuery->num_rows() < 1){
			//there is no unsent message
			continue; //next loop
		try {
			//connect to gtalk server
			//send messages
			foreach ($messageQuery->result() as $row){
				$recipient = $row->recipient;
				$message = $row->message;
				//$receipient contains your receipent GTalk email, eg. ''
				$conn->message($recipient, $message);
				//update notification status to sent				
			//disconnect from gtalk server
		} catch(XMPPHP_Exception $e) {
			//somewhere in between, error occurs
		    echo "Error occur while sending message. Message: " . $e->getMessage() ;
} // End of function

Running the script from command line:

cd /path/to/your/CI/index.php
/path/to/php index.php notification sendNotificationCronJob