I understand with an example from Google about how to implement GMS and there is such a piece of code that I send messages to GMS, but I can't understand why send messages to GMS? This code block is tied to a button and it is not clear what it tests when it is pressed? What and why is sent to the server?

public void onClick(final View view) { if (view == findViewById(R.id.send)) { new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... params) { try { Bundle data = new Bundle(); data.putString("my_message", "Hello World"); data.putString("my_action", "com.google.android.gcm.demo.app.ECHO_NOW"); String id = Integer.toString(msgId.incrementAndGet()); ---->>> gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data); msg = "Sent message"; } catch (IOException ex) { } } 

As far as I understand, this is not the registration of the device in the GCM since the registration of the device occurs in this block

 private void registerInBackground() { new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... params) { String msg = ""; try { if (gcm == null) { gcm = GoogleCloudMessaging.getInstance(context); } registrationId = gcm.register(SENDER_ID); msg = "Device registered, registration ID=" + registrationId; // You should send the registration ID to your server over HTTP, so it // can use GCM/HTTP or CCS to send messages to your app. sendRegistrationIdToBackend(); // For this demo: we don't need to send it because the device will send // upstream messages to a server that echo back the message using the // 'from' address in the message. // Persist the regID - no need to register again. storeRegistrationId(context, registrationId); } catch (IOException ex) { msg = "Error :" + ex.getMessage(); // If there is an error, don't just keep trying to register. // Require the user to click a button again, or perform // exponential back-off. } return msg; } @Override protected void onPostExecute(String msg) { mDisplay.append(msg + "\n"); } }.execute(null, null, null); } 
  • Your server sends to GCM, and it already delivers directly to the client (phone / tablet). - KoVadim
  • Not exactly what concerns my question. I corrected the question, please see - Aleksey Timoshchenko

1 answer 1

1) For registration in GCM from the client we send senderId and applicationId
2) If the registration process is successful in GCM, we get a response- registrationId 3) After we send the received registrationId to our server and save it to the database. 4) Well, the server will send pushes with messages to the GCM server, to the specified id-Schnick.

It seems to be something like that

  • That's right, you described it, but it doesn’t have anything to do with my question ... You can see if I corrected the question - Aleksey Timoshchenko
  • There in the commentary in English it is written that this message will go to itself. That is just to test the notification example, I think - Android Android
  • Can you explain in more detail where it is written and how it relates to gcm.send ? What kind of notification are they testing? I'm just a beginner and I don’t understand this answer at all ... I see that sending register and receiving registrationId happens automatically and the second thing I see is a button and a function attached to it that does gcm.send ... Why don’t I understand? so what should gcm do in this case? If Google indicated in his example, why and how to work with it? - Aleksey Timoshchenko