到服务器应用程序的上游消息
Upstream message to server app
我已经使用 JAXL 成功地将数据从 php 服务器页面发送到 android 客户端..
我已经仔细阅读了Google云消息官网的指南。.上游只有这些文件:
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("");
}
}
这样说:
Receive XMPP messages on the app server
When GCM receives an upstream messaging call from a client app, it generates the necessary XMPP stanza for sending the upstream message.
GCM adds the category and from fields, and then sends a stanza like
the following to the app server:
<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 时,没有被问及应用程序服务器。发件人 ID identfy 我的 gmail 帐户的应用程序包。不是应用服务器。那么 gcm 将客户端发送的数据发送到哪里? GCM 如何知道我的应用服务器..
2-)我的预算有限,我的服务器是共享帐户网络服务器。所以我必须使用 php...但是我已经阅读了文档,"Your app server should be persistent connection" 不能定期连接和断开连接...我可以将应用程序服务器用作 php 吗? GCM 连接 Php scrpit 来评估数据并响应 android 客户端?
如您所知,您的服务器和 GCM 之间的连接需要发件人 ID 和 API 密钥。此外,当客户端应用程序想要发送上游消息时,它会使用相同的发送者 ID。所以 GCM 知道应该将上游数据发送给谁。
你的编程语言没有限制,当然可以用PHP。您只需要保持与 GCM 服务器的持久连接。
不关心某些断开连接,请注意 Google 将在您的服务器关闭时重试发送上游消息,并且不会针对特定消息将 ACK 发送回 GCM。
以下是我如何设法上传消息...
首先得到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,您将看到 "Hello World" 消息由应用发送
@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
}
});
}
我已经使用 JAXL 成功地将数据从 php 服务器页面发送到 android 客户端..
我已经仔细阅读了Google云消息官网的指南。.上游只有这些文件:
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("");
}
}
这样说:
Receive XMPP messages on the app server
When GCM receives an upstream messaging call from a client app, it generates the necessary XMPP stanza for sending the upstream message.
GCM adds the category and from fields, and then sends a stanza like the following to the app server:
<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 时,没有被问及应用程序服务器。发件人 ID identfy 我的 gmail 帐户的应用程序包。不是应用服务器。那么 gcm 将客户端发送的数据发送到哪里? GCM 如何知道我的应用服务器..
2-)我的预算有限,我的服务器是共享帐户网络服务器。所以我必须使用 php...但是我已经阅读了文档,"Your app server should be persistent connection" 不能定期连接和断开连接...我可以将应用程序服务器用作 php 吗? GCM 连接 Php scrpit 来评估数据并响应 android 客户端?
如您所知,您的服务器和 GCM 之间的连接需要发件人 ID 和 API 密钥。此外,当客户端应用程序想要发送上游消息时,它会使用相同的发送者 ID。所以 GCM 知道应该将上游数据发送给谁。
你的编程语言没有限制,当然可以用PHP。您只需要保持与 GCM 服务器的持久连接。
不关心某些断开连接,请注意 Google 将在您的服务器关闭时重试发送上游消息,并且不会针对特定消息将 ACK 发送回 GCM。
以下是我如何设法上传消息...
首先得到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,您将看到 "Hello World" 消息由应用发送
@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
}
});
}