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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//M_notification_queue.php file
...
function getUnsendNotifications(){
    //Load the database
    $this->load->database();
     
    //Build the sql query
    //Retrieve unsent messages only
    $this->db->where('status','unsent');
     
    //Retrieve from table notification
    $query = $this->db->get('notification');
     
    //return the active record handler
    return $query;
}
...

Implement the sending controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//notification.php controller file
private $gtalk_username = "sender@gmail.com"; //fake email
private $gtalk_password = "password"; //fake password
private $check_notification_interval = 10; //frequency to check for unsent message
...
 
function sendNotificationCronJob(){
    //load required models
    $this->load->model('M_notification');
     
    //initialize the xmpp library
    $conn = new XMPPHP_XMPP(
        'talk.google.com', //google talk xmpp server address
        5222, //port
        $this->gtalk_username , //username
        $this->gtalk_password, //password
        'xmpphp', //client
        'gmail.com', //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
    while(1){      
        //pause execution
        sleep($this->check_notification_interval);
         
        //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
            $conn->useEncryption(true);
            $conn->connect();
            $conn->processUntil('session_start');
            $conn->presence();
         
            //send messages
            foreach ($messageQuery->result() as $row){
                $recipient = $row->recipient;
                $message = $row->message;
                 
                //$receipient contains your receipent GTalk email, eg. 'receipient@gmail.com'
                $conn->message($recipient, $message);
                 
                //update notification status to sent               
                ...
            }
             
            //disconnect from gtalk server
            $conn->disconnect();
             
        } 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:

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