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:
Create a project at Google Developer Console;
- Activate "Cloud Messaging for Android" API;
- Create a server API key at "Credentials";
- Take note of the project ID;
- Take note of the project number;
- Take note of the server API key;
Create a Android Studio project;
- Add an "App Engine Backend with CGM" module type;
- On the "appengine-web.xml" window that will open, type in the application ID and the server API Key.
- 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>