我成功地从我的应用服务器(本地主机)向我的安卓手机发送了通知。我一直在阅读并尝试不同的东西来实现服务器端代码,这样我就可以使用谷歌云消息来实现上游消息。我查看了此上游消息到服务器应用程序,然后我在我的 android 手机上使用了代码,我收到“已发送消息”,但我不明白我的服务器如何以及是否真的收到了消息。
所以我的问题是我的服务器端实现正确吗?(现在我正在使用运行 localhost 的 WAMP)我在哪里可以找到 Jaxl 生成的日志输出?
我已经尝试了 2-3 天(是的,我阅读了谷歌云文档和 Jaxl 入门文档,但我仍然不清楚)。ps 我没有足够的声誉点来评论我上面提供的链接,这就是我创建一个新问题的原因。
更新
好的,所以今天突然间,Jaxl 实际上在我的本地主机文件存储的同一目录中创建了一个日志文件 C:\wamp\www\myproject ,但是当我从我自己的 PC 访问它时它实际上是在创建日志。日志
jaxl_fsm:61 - 2016-04-29 07:25:33 - calling state handler 'setup' for
incoming event 'connect'
jaxl_socket_client:95 - 2016-04-29 07:25:33 - trying tcp://gcm-preprod.googleapis.com:5236
jaxl_socket_client:104 - 2016-04-29 07:25:37 - connected to tcp://gcm-preprod.googleapis.com:5236
jaxl_loop:82 - 2016-04-29 07:25:37 - active read fds: 1, write fds: 0
jaxl_fsm:71 - 2016-04-29 07:25:37 - current state 'connected'
jaxl_fsm:61 - 2016-04-29 07:25:37 - calling state handler 'connected' for incoming event 'start_stream'
jaxl_loop:82 - 2016-04-29 07:25:37 - active read fds: 1, write fds: 1
jaxl_fsm:71 - 2016-04-29 07:25:37 - current state 'wait_for_stream_start'
jaxl_socket_client:201 - 2016-04-29 07:25:37 - sent 186/186 of data
jaxl_socket_client:202 - 2016-04-29 07:25:37 - <stream:stream xmlns:stream="http://etherx.jabber.org/streams" version="1.0" to="gcm.googleapis.com" xmlns="jabber:client" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace">
jaxl_loop:104 - 2016-04-29 07:25:37 - active read fds: 1, write fds: 0
jaxl_socket_client:188 - 2016-04-29 07:25:37 - read 7/7 of data
jaxl_socket_client:189 - 2016-04-29 07:25:37 - F
jaxl_socket_client:175 - 2016-04-29 07:25:37 - socket eof, disconnecting
jaxl_loop:104 - 2016-04-29 07:25:37 - active read fds: 0, write fds: 0
jaxl_loop:104 - 2016-04-29 07:25:37 - active read fds: 0, write fds: 0
jaxl_loop:115 - 2016-04-29 07:25:37 - no more active fd's to select
这是我的 jaxl php 代码:
<?php
include_once 'jaxl.php';//to use JAXL librabry
$client = new JAXL(array(
'jid' => '/Projectid/@gcm.googleapis.com',
'pass' => '', //API key
'host' => 'gcm-preprod.googleapis.com',
'port' => 5236,
'strict' => false,
'force_tls' => true,
'log_level' => JAXL_DEBUG,
'auth_type' => 'PLAIN',
'protocol' => 'tls',
'ssl' => TRUE,
'log_path'=> 'myUpstreamlog.txt' /*This create text file to comminication between gcm and your server*/
));
$client->add_cb('on__message_stanza', function($msg) {
echo 'now what!!';
});
$client->add_cb('on_auth_success', function() {
// echo 'it should';
//Here is for sending downstream msg
//registration token of my android phone
$reg_token = array('fy6HF-kKO3M:APA91bGO3F0BKHk6nfPpwf4iLJAZgLag2ZL7uRyRC2vHyE_hmgRCaaj2E5PbhobN0ki7_rfEfOyUjD9-5ml064mULKynalDt69G1FmY_k2CnalMRe-eFzUswPjUrx5yxCZOUfI3tsFSc');
//Creating a message array
$msg = array
(
'hello this is your server'
);
//send back to phone
$fields = array
(
'to' => $reg_token,
'message_id' => 1,
'data' => $msg,
'time_to_live' => 600 ,
'delay_while_idle'=> true,
'delivery_receipt_requested' => true
);
//Using curl to perform http request
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
//Getting the result
$result = curl_exec($ch );
curl_close( $ch );
//Decoding json from result
$res = json_decode($result);
$myfile = fopen("sendAcktoClient.txt", "w");
fwrite($myfile, $res);
fclose($myfile);
});
$client->add_cb('on_error_message',function()
{
global $client;
echo 'error<br/>';
_info('got on_error_message cb jid'.$client->full_jid->to_string());
});
$client->start();
?>
我尝试更改客户端(Android 手机)发送的消息 ID,但我没有收到任何新日志。
这是我客户的代码
package com.example.meer.bustedtracking;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import java.io.IOException;
/**
* Created by User on 4/26/2016.
*/
public class SendFromClient extends AsyncTask<String,Void,String>{
private Context context;
private String SENDER_ID=""; //project id
public SendFromClient(Context ctx){context=ctx;}
@Override
protected String doInBackground(String... params) {
String msg = "";
String id="1";//this should be unique for each msg
final GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
try {
Bundle data = new Bundle();
data.putString("my_message", "Hello World");
data.putString("my_action","SAY_HELLO");
// String id = Integer.toString(msgId.incrementAndGet());
gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data);
msg = "Sent message "+ id;
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
Log.i("SendFromClient ",msg );
}
}