我已成功将数据从php服务器页面发送到带有JAXL的Android客户端..
我仔细阅读了Google Cloud Message Offical网站的指南..对于Upstream,只有这些文件:
public void onClick(final View view) {
if (view == findViewById(R.id.send)) {
new AsyncTask() {
@Override
protected String doInBackground(Void... params) {
String msg = "";
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";
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
}
@Override
protected void onPostExecute(String msg) {
mDisplay.append(msg + "\n");
}
}.execute(null, null, null);
} else if (view == findViewById(R.id.clear)) {
mDisplay.setText("");
}
}
说出来:
在应用服务器上接收XMPP消息
When GCM receives an upstream messaging call from a client app, it generates the necessary XMPP stanza for sending the upstream message.
GCM添加类别和字段,然后发送类似的节 以下内容到app服务器:
<message id="">
<gcm xmlns="google:mobile:data">
{
"category":"com.example.yourapp", // to know which app sent it
"data":
{
"hello":"world",
},
"message_id":"m-123",
"from":"REGID"
}
</gcm>
</message>
但现在我有一些问题,因为上游的文件有限。
1-)Android发送JSON数据,发送者ID为上游...但是当我注册到api时,没有被问及app server。发件人ID通过包识别我的Gmail帐户的应用程序。不是app服务器。那么gcm发送客户来的数据呢?如何让GCM了解我的应用服务器..
2-)我预算有限,我的服务器是共享帐户Web服务器。所以我必须使用php ...但我已阅读文档,“您的应用服务器应该是持久连接”而不是连接&amp;定期断开...我可以使用app服务器作为PHP吗? GCM连接Php scrpit,它可以排出数据并回复到Android客户端吗?
答案 0 :(得分:2)
您可能知道,服务器和GCM之间的连接需要sender-id和API密钥。此外,当客户端应用程序想要发送上游消息时,它使用相同的sender-id。所以GCM知道它应该向谁发送上游数据。
您的编程语言没有限制,当然您可以使用PHP。您只需要维护与GCM服务器的持久连接。
不要关心某些断开连接,请注意,如果您的服务器已关闭,Google将重试发送上游消息,并且没有向特定消息的GCM发送ACK。
答案 1 :(得分:1)
以下是我如何管理上游消息...
首先获得JAXL
把它放在你的apache执行目录...
创建新的php脚本文件......
<?php
include_once 'jaxl.php';
$client = new JAXL(array(
'jid' => '/*Write sender ID here*/@gcm.googleapis.com',
'pass' => 'Write here your GCM apı 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'=> 'ex.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
});
$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部分,在你与GCM集成之后,一个带有内部点击监听器的按钮
String msg = "";
try {
Bundle data = new Bundle();
data.putString("my_message", "Hello World");
data.putString("my_action", "SAY_HELLO");
String id = Integer.toString(incrementAndGet());
gcm.send( "/*Write here sender ID*/"+ "@gcm.googleapis.com", id, data);
msg = "Sent message";
} catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
Log.d(msg,"-------------");
然后,执行你上面写的php脚本,然后单击按钮以发送上游消息,看看jaxl创建的ex.txt,你会看到app发送的“Hello World”消息
答案 2 :(得分:1)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "FCM Token creation logic");
// Get variables reference
deviceText = (TextView) findViewById(R.id.deviceText);
editTextEcho = (EditText) findViewById(R.id.editTextEcho);
buttonUpstreamEcho = (Button) findViewById(R.id.buttonUpstreamEcho);
//Get token from Firebase
FirebaseMessaging.getInstance().subscribeToTopic("test");
final String token = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Token: " + token);
deviceText.setText(token);
//Call the token service to save the token in the database
tokenService = new TokenService(this, this);
tokenService.registerTokenInDB(token);
buttonUpstreamEcho.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Echo Upstream message logic");
String message = editTextEcho.getText().toString();
Log.d(TAG, "Message: " + message + ", recipient: " + token);
FirebaseMessaging.getInstance().send(new RemoteMessage.Builder(FCM_PROJECT_SENDER_ID + FCM_SERVER_CONNECTION)
.setMessageId(Integer.toString(RANDOM.nextInt()))
.addData("message", message)
.addData("action", BACKEND_ACTION_ECHO)
.build());
// To send a message to other device through the XMPP Server, you should add the
// receiverId and change the action name to BACKEND_ACTION_MESSAGE in the data
}
});
}