I have successfuly send data from php server page to android client with JAXL..
I have read carefully the guide of Google Cloud Message Offical website.. For Upstrea
As you may know, the connection between your server and GCM required sender-id and API key. Also when a client app wants to send an upstream message, it uses the same sender-id. So GCM knows to whom it should send the upstream data.
There is no limit on your programming language, of course you can use PHP. You just have to maintain a persistent connection to GCM server.
Don't care about some disconnections, note that Google will retry to send the upstream messages if your server was down, and didn't send an ACK back to GCM for a particular message.
Here is how I manage to upstream message...
First get JAXL
put it on your apache execute directory...
create new php script file...
<?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();
?>
On android part, after you did integrate with GCM, A button with inside click listener
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,"-------------");
Then, execute your php script, that writed above, then click button in order to send upstream msg, look ex.txt that jaxl created, you will see "Hello World" msg that sent by app
@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
}
});
}
GitHub: https://github.com/carlosCharz/FCMTest