Upstream message to server app

1.1k views Asked by At

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 Upstream there is only these documents:

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

Say this:

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>

But now I have some questions,because of limited documents for upstream.

1-)Android send JSON data, with sender id for upstream... But when I register to api,have not been asked about app server. Sender Id identfy my gmail account's application with package. Not app server. so where gcm send data that come by client? How to GCM knows my app server..

2-)I have limited budget, my server is shared account web server. So I have to use php... But I have read on document,"Your app server should be persistent connection" not connect& disconnect regularly... Can I use app server as php? that GCM connects Php scrpit which evulate data and respond back to android client?

3

There are 3 answers

4
Ali On BEST ANSWER
  1. 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.

  2. 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.

4
Ucdemir On

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

1
Carlos Becerra Rodriguez On
@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