2

I can connect to the server but cannot read the acknowledgements messages sent by the gcm server. I want to know how to retrieve the XML sent by gcm server. Here is my code :

<?php
require 'jaxl/jaxl.php';
$sender = 'sender id';
$client = new \JAXL(array(
 'jid'=>$sender,
 'pass'=>'pass',
 'auth_type'=>'PLAIN',
 'host' => 'gcm.googleapis.com',
 'port' => '5235',
 'force_tls' => true,
 'strict' => FALSE,
 'ssl' => TRUE,
 'log_level' => JAXL_DEBUG,
 'log_path'=> 'ex.txt'
  )); 

  $client->add_cb('on_message_stanza', function($msg) {
 echo 'now what!!';
 });
 $client->add_cb('on_auth_success', function() {
 echo 'it should';
 global $client;

 global $sender;
 $namespace = 'google:mobile:data';
 $arr=array("hello"=>"world");
 $json_data=json_encode($arr,JSON_UNESCAPED_SLASHES);
 $arr = array('to'=>'server','message_id'=>'123','data'=>$arr);
 $json = json_encode($arr);
 $xml = new JAXLXml('gcm',$namespace,array(),$json);
 $msg = new JAXLXml('message','',array('id'=>'123'),'');
 $msg->cnode($xml);
 $client->send($msg);
  }); 
 $client->add_cb('on_error_message',function()
 {
 global $client;
 echo 'error<br/>';
 _info('got on_error_message cb jid'.$client->full_jid->to_string());
 });

In the callback 'on_auth_success', i am sending a message to gcm server directed to my server id, it is sending a negative acknowledgement 'nack' which i can see in the log, but I don't know how to receive that in php code. The XML received by gcm as per log is :

 <message>
 <data:gcm xmlns:data="google:mobile:data">{"message_id":"123","from":"someid",
 "error_description":"","error":"BAD_REGISTRATION",
 "message_type":"nack"}</data:gcm></message>
Anupam
  • 55
  • 6

2 Answers2

2

Oh, I got it, since the response message didn't had any type, I have to add the callback 'on__message', with 2 underscore, because middle value is the value of type attribute of the message which the response don't have.

Anupam
  • 55
  • 6
0

For an ACK, you can use message type 'normal', so the cb would look like below (I am just logging response):

$client->add_cb('on_normal_message', function($stanza) {
     global $client;
    // echo back incoming message stanza
    //$stanza->to = $stanza->from;
    //$stanza->from = $client->full_jid->to_string();
    //$client->send($stanza);
    _info('Received response******'.$stanza->to_string());
});

Also: in the example, you need to add

$client->start();

For us newbies.

Then it works fine with GCM.

cchapman
  • 2,668
  • 6
  • 42
  • 59