【问题标题】:Cannot Read GCM server response using JAXL php library无法使用 JAXL php 库读取 GCM 服务器响应
【发布时间】:2015-02-09 17:48:43
【问题描述】:

我可以连接到服务器,但无法读取 gcm 服务器发送的确认消息。我想知道如何检索 gcm 服务器发送的 XML。 这是我的代码:

<?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());
 });

在回调“on_auth_success”中,我正在向 gcm 服务器发送一条指向我的服务器 ID 的消息,它正在发送我可以在日志中看到的否定确认“nack”,但我不知道如何接收在 php 代码中。 gcm 根据日志收到的 XML 是:

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

【问题讨论】:

    标签: php xmpp google-cloud-messaging jaxl


    【解决方案1】:

    哦,我明白了,因为响应消息没有任何类型,所以我必须添加回调'on__message',下划线2,因为中间值是响应不的消息的类型属性值没有。

    【讨论】:

      【解决方案2】:

      对于 ACK,您可以使用消息类型“正常”,因此 cb 如下所示(我只是记录响应):

      $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());
      });
      

      另外:在示例中,您需要添加

      $client->start();
      

      对于我们新手。

      然后它与 GCM 一起工作正常。

      【讨论】:

        猜你喜欢
        • 2012-03-30
        • 2015-07-18
        • 1970-01-01
        • 2012-12-07
        • 2012-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-09
        相关资源
        最近更新 更多