код ниже, так это делает хороший основной рабочий пример того, как использовать GCM
Вот соответствующие части я добавил в манифесте:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="20" />
<permission
android:name=".permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name=".permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
...
<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="com.badbob.app.gmctestapp" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
Я добавил следующий код в OnCreate моей основной деятельности:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if(regId.equals("")) {
GCMRegistrar.register(this, GCM_SENDER_ID);
}
else {
Log.v(LOG_TAG, "Already registered");
}
Я также создал класс GCMIntenetService так:
public class GCMIntentService extends GCMBaseIntentService {
private static final String LOG_TAG = "GetAClue::GCMIntentService";
public GCMIntentService() {
super(GCM_SENDER_ID);
// TODO Auto-generated constructor stub
Log.i(LOG_TAG, "GCMIntentService constructor called");
}
@Override
protected void onError(Context arg0, String errorId) {
// TODO Auto-generated method stub
Log.i(LOG_TAG, "GCMIntentService onError called: " + errorId);
}
@Override
protected void onMessage(Context arg0, Intent intent) {
// TODO Auto-generated method stub
Log.i(LOG_TAG, "GCMIntentService onMessage called");
Log.i(LOG_TAG, "Message is: " + intent.getStringExtra("message"));
}
@Override
protected void onRegistered(Context arg0, String registrationId) {
// TODO Auto-generated method stub
Log.i(LOG_TAG, "GCMIntentService onRegistered called");
Log.i(LOG_TAG, "Registration id is: " + registrationId);
}
@Override
protected void onUnregistered(Context arg0, String registrationId) {
// TODO Auto-generated method stub
Log.i(LOG_TAG, "GCMIntentService onUnregistered called");
Log.i(LOG_TAG, "Registration id is: " + registrationId);
}
}