1

I have been struggling with GCM implementation for some weeks now, but how I really want to get to understand how it works I decided to take 'baby steps'.

First of all, as mentioned here, I understood that the first thing to do is register my device/app must first register with GCM.

To verify that they can send and receive messages, client apps must register with GCM. In this process, the client obtains a unique registration token.

I'd like to know if the procedure and code below represent the very minimal code necessary to make such registration (the 'very minimal code necessary' is because when I learned OpenGL ES 2.0 and started to deal with shaders, I saw that when dealing with hard/confusing concepts, If you understand the minimal code necessary you can later understand what the "peripheral" code)

Procedure to registrate the app with GCM:

  1. Create a project at Google Developer Console;

    1. Activate "Cloud Messaging for Android" API;
    2. Create a server API key at "Credentials";
    3. Take note of the project ID;
    4. Take note of the project number;
    5. Take note of the server API key;
  2. Create a Android Studio project;

    1. Add an "App Engine Backend with CGM" module type;
    2. On the "appengine-web.xml" window that will open, type in the application ID and the server API Key.
    3. Create a main activity, using the project number (SENDER_ID).

After I didn all of that and run the app, I got a token with 152 characters. Is all of that correct? Considering that I got a token back, is my device registered with GCM?

appengine-web.xml:

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>mygcmtest...</application>
    <version>1</version>
    <threadsafe>true</threadsafe>

    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />

        <property name="gcm.api.key" value="AIza..." />
    </system-properties>
</appengine-web-app>

MainActivity:

public class MainActivity extends AppCompatActivity {

    private final Context mContext = this;
    private final String SENDER_ID = "319899...";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getGCMToken();
    }

    private void getGCMToken() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    InstanceID instanceID = InstanceID.getInstance(mContext);
                    String token = instanceID.getToken(SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
                    Log.e("GCM Token", token);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute();
    }
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aninha.mygcmtest..." >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="com.example.gcm.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Mark McDonald
  • 7,571
  • 6
  • 46
  • 53
Danilo Setton
  • 601
  • 10
  • 20

1 Answers1

3

Is my answer with source code at your question How to implement a GCM Hello World for Android using Android Studio is not enough for you to create two simple GCM projects (server-side and client-side) yet? :)

Of course, the sample code I used is just for a very basic case "server app sends, client app receives and displays message".

To sum up, you need to do the following steps:

  1. Create a new project at Google Developers Console . At this step, for simplicity, you just need to take note of 2 values: Project Number, which will be used as SENDER_ID in my client project; and API server key (created at Credentials), which will be used as API_KEY in my server project.
  2. Create a new simple Android project for server side (with basic source code as my answer in your previous question).
  3. Create a new simple Android project for client side (with basic source code as my answer in your previous question, I customized from the original source at Google Cloud Messaging - GitHub).
  4. Run the client app, you will get the registration token (means that your device has successfully registered). Then, paste (hard-code) this token at CLIENT_REGISTRATION_TOKEN variable in server app.
  5. Run the server app, and check the result (client app received the message or not)

Hope this helps!

P/S: I don't use any appengine-web.xml file

Community
  • 1
  • 1
BNK
  • 23,994
  • 8
  • 77
  • 87
  • 1
    your first reply on the other post was great, it's just that i got a little confused with two MainActivity classes and some other minor stuff. Now it's much clear for me. I'll try to implement the code today and I'll let you know the result – Danilo Setton Sep 01 '15 at 09:19
  • @BKN I think everything went OK, I just received a strange message on the TextView..."{ "failure": 1, "results": [ { "error": "NotRegistered"}], "success": 0, "multicast_id": 322984...., "canonical_ids":0}. That's the message I was supposed to receive? – Danilo Setton Sep 02 '15 at 01:24
  • 1
    For error response code, go to [this reference](https://developers.google.com/cloud-messaging/http-server-ref#error-codes). Perhaps for "NotRegistered" error, you should clear data of installed client app to get a new registration token, then paste this new token as in step #4 – BNK Sep 02 '15 at 03:22
  • @BKN the message has changed, but is this what I was supposed to receive? { "failure": 0, "results": [ { "message_id": "0:144...." } ] , "sucess":1, "multicast_id": 3763...., "canonical_ids": 0} – Danilo Setton Sep 03 '15 at 00:24
  • 1
    It means that server app successfully sent. However, did your client app receive any message from the server app or not? – BNK Sep 03 '15 at 00:32
  • @BKN yes mate, I received the message. Let me ask you something...I ran the client app, and then the server app. On the server activity, when I pressed the back button I went back to the client app, and then I saw the message. Then I closed all the apps and opened again the client app, but I didn't see the message. The message was "consumed" on the first time I received it? Anyway, THANKS A LOT FOR YOUR HELP! – Danilo Setton Sep 03 '15 at 00:51
  • 1
    That depend on how you display message on client app, since GCMService is a service, when closing client app by Back button, for example, if using Toast to display, message still displays, if setText in TextView, of course at that time TextView is null, cannot setText. If want to save message for future use, you can use logging saves to a file, or using shared ref or SQLLite db to store – BNK Sep 03 '15 at 00:59