问题
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>
回答1:
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:
- 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 asSENDER_ID
in my client project; andAPI server key
(created at Credentials), which will be used asAPI_KEY
in my server project. - Create a new simple Android project for server side (with basic source code as my answer in your previous question).
- 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).
- 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. - 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
来源:https://stackoverflow.com/questions/32322631/adding-google-cloud-messagin-gcm-for-android-registration-process