In sostanza, si dovrebbe effettuare le seguenti operazioni nella vostra attività principale:
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");
}
}
Successivamente è necessario inviare l'id di registrazione al server app, ogni volta l'applicazione riceve un com.google.android.c2dm.intent.REGISTRATION
intenti con un registration_id
supplementare. Ciò potrebbe accadere quando Google aggiorna periodicamente l'ID dell'app.
È possibile raggiungere questo estendendo com.google.android.gcm.GCMBaseIntentService
con una propria implementazione, ad es .:
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.
}
}
Per maggiori dettagli, vi (ri) leggere la documentazione per writing the client side code e the Advanced Section of the GCM documentation.
Spero che questo aiuti!
possibile duplicazione di [Modifica ID registrazione ID in Google Cloud Messaging su Android] (http://stackoverflow.com/questions/16838654/handling-registration-id-changes-in-google-cloud-messaging-on -android) – Eran