我使用此代码获取gcm注册ID。 (的早些时候)
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
// registerReceiver(mHandleMessageReceiver, new IntentFilter(
// CommonUtilities.DISPLAY_MESSAGE_ACTION));
// Get GCM registration id
final String regId = GCMRegistrar.getRegistrationId(this);
// Check if regid already presents
if (regId.equals("")) {
// Registration is not present, register now with GCM
GCMRegistrar.register(this, CommonUtilities.SENDER_ID);
} else {
AppSharedPreferences.saveDeviceIDPreference(ctx, regId);
// Device is already registered on GCM
if (GCMRegistrar.isRegisteredOnServer(this)) {
// Skips registration.
// Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
} else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// Register on our server
// On server creates a new user
ServerUtilities.register(context, regId);
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
但是,令人震惊的是,它似乎每次执行都会改变, for the same device and same application
。请告诉我可能的原因。
EDITED
将GCM修改为较新版本
gcm = GoogleCloudMessaging.getInstance(this);
new RegisterBackground().execute();
但是,仍然有相同的行为,即每次安装都会获得新的注册。