1

I am doing work on GCM (Google Cloud Messaging) in Android. I am looking for the upstream message using GCM.

Code send the GCM messages to cloud here

try {
   Bundle data = new Bundle();
   // the account is used for keeping 
   // track of user notifications
   data.putString("account", account);
   // the action is used to distinguish 
   // different message types on the server
   data.putString("action", Constants.ACTION_REGISTER);
   String msgId = Integer.toString(getNextMsgId());
   gcm.send(projectId + "@gcm.googleapis.com", msgId,
         Constants.GCM_DEFAULT_TTL, data);
} catch (IOException e) {
   Log.e("grokkingandroid",
         "IOException while sending registration id", e);
}

Now question is that what cloud would do for that upstream message, Where it can be useful in Android and How ??

Eran
  • 387,369
  • 54
  • 702
  • 768
N Sharma
  • 33,489
  • 95
  • 256
  • 444

2 Answers2

3

When you send an upstream message from your app, the GCM Cloud Connection Server (CCS) transfers that message to your server. In order for that to work, you must implement a server that supports XMPP protocol and establishes a TLS connection with GCM Cloud Connection Server. You also need your API project to be white-listed for using this feature. You can read more about it here.

As for usefulness, it allows you to send messages to your app via the GCM connection instead of via your own connection between your app and your server. That's more battery efficient.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Thank you !. How GCM Cloud Connection Server will make connection with my server where it support PHP so that I can run the PHP script on whatever message got from the app. – N Sharma Apr 10 '14 at 05:04
  • @Williams GCM CCS won't connect to your server. It's your server's responsibility to establish and keep that connection alive. The projectID that you supply when you send the message tells GCM CCS which server the message should be delivered to. As for using PHP, you'll have to find some PHP library that supports the XMPP protocol. – Eran Apr 10 '14 at 17:54
  • Thank you, do you have any example link that communicate my server to GCM CCS using PHP ? – N Sharma Apr 11 '14 at 09:49
  • @Williams No I don't. I don't know PHP. – Eran Apr 11 '14 at 11:40
  • Okay No problem but anyway nice answer – N Sharma Apr 11 '14 at 11:40
2
@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
        }
    });

}

This is a sample Android project to showcase the Firebase Cloud Messaging (FCM) to manage upstream and downstream messages.

https://github.com/carlosCharz/FCMTest

This is the video in youtube that explains what it does.

https://www.youtube.com/watch?v=SEzOKSoAMG0

Hope you find it useful.