Adding Google Cloud Messagin (GCM) for Android - Registration process

▼魔方 西西 提交于 2019-12-17 06:49:15

问题


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>

回答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:

  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



来源:https://stackoverflow.com/questions/32322631/adding-google-cloud-messagin-gcm-for-android-registration-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!