这里解释:http://developer.android.com/guide/google/gcm/gs.html
编写 Android 应用程序
您必须下载并添加 gcm 库。
然后在清单中进行更改:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="xx"/>
和
<permission android:name="my_app_package.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="my_app_package.permission.C2D_MESSAGE" />
在“my_app_package”中插入你的包名。
添加其他权限
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
接收者
<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="my_app_package" />
</intent-filter>
</receiver>
再次在“my_app_package”中写下你的包名。
然后声明你的服务
<service android:name=".GCMIntentService" />
您必须创建一个名为 GCMIntentService 的类。这个类将处理推送通知。
它应该是这样的:
class GCMIntentService extends com.google.android.gcm.GCMBaseIntentService{
public void onRegistered(Context context, String regId){}
public void onUnregistered(Context context, String regId){}
public void onError(Context context, String errorId){}
public void onMessage(Context context, Intent intent){}
}
在 onMessage() 中,您必须处理推送消息。您可以使用通知。
然后在您的开始活动中,您将不得不把这个
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
在 onCreate() 中。
SENDER_ID 是包含您从 google 控制台获得的所有数字的字符串。