Zasadniczo, należy wykonać następujące czynności w swojej głównej działalności:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, GCMIntentService.GCM_SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
}
Następnie należy wysłać identyfikator rejestracji na serwerze aplikacji, ilekroć aplikacja odbiera com.google.android.c2dm.intent.REGISTRATION
zamiaru z registration_id
extra. Może się to zdarzyć, gdy Google okresowo aktualizuje identyfikator aplikacji.
Można to osiągnąć poprzez rozszerzenie com.google.android.gcm.GCMBaseIntentService
z własnej implementacji, np .:
public class GCMIntentService extends GCMBaseIntentService {
// Also known as the "project id".
public static final String GCM_SENDER_ID = "XXXXXXXXXXXXX";
private static final String TAG = "GCMIntentService";
public GCMIntentService() {
super(GCM_SENDER_ID);
}
@Override
protected void onRegistered(Context context, String regId) {
// Send the regId to your server.
}
@Override
protected void onUnregistered(Context context, String regId) {
// Unregister the regId at your server.
}
@Override
protected void onMessage(Context context, Intent msg) {
// Handle the message.
}
@Override
protected void onError(Context context, String errorId) {
// Handle the error.
}
}
Aby uzyskać więcej szczegółów, chciałbym (re) zapoznać się z dokumentacją dla writing the client side code i the Advanced Section of the GCM documentation.
Nadzieję, że pomaga!
możliwe duplikat [Handling zmiany identyfikator rejestracji w Google Cloud Messaging na Androidzie] (http://stackoverflow.com/questions/16838654/handling-registration-id-changes-in-google-cloud-messaging-on -android) – Eran