几周以来,我一直在努力实现GCM,但我真的想了解它是如何工作的我决定采取“婴儿步骤”. 首先,正如here所述,我明白首先要做的是注册我的设备/应用程序必须先注册GCM. To verify that they
首先,正如here所述,我明白首先要做的是注册我的设备/应用程序必须先注册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.
我想知道下面的过程和代码是否代表了进行此类注册所需的极小代码(“非常少的代码必需”是因为当我学习OpenGL ES 2.0并开始处理着色器时,我在交易时看到了困难/困惑的概念,如果您了解必要的最小代码,您可以稍后了解“外围”代码是什么
使用GCM注册应用程序的过程:
>在Google Developer Console中创建项目;
>激活“适用于Android的云消息传递”API;
>在“凭据”中创建服务器API密钥;
>记下项目ID;
>记下项目编号;
>记下服务器API密钥;
>创建一个Android Studio项目;
>添加“带CGM的App Engine后端”模块类型;
>在将打开的“appengine-web.xml”窗口中,键入应用程序ID和服务器API密钥.
>使用项目编号(SENDER_ID)创建主要活动.
在我完成所有操作并运行应用程序之后,我得到了一个包含152个字符的令牌.
这一切都是正确的吗?考虑到我收到了令牌,我的设备是否已在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>
主要活动:
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();
}
}
表现:
<?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>
我在你的问题
How to implement a GCM Hello World for Android using Android Studio中使用源代码的答案是不是足以让你创建两个简单的GCM项目(服务器端和客户端)呢? 