0
在服務器端,我發現一個設備有多個註冊ID,這顯然是爲我創造了很多問題。像收到很多次的消息。GCM:服務器端的多個應用程序ID
我怎麼能得到舊的註冊ID紅色,或確保註冊沒有發生,如果有一個有效的註冊ID。
我跟隨在Android文檔的實例教程的時候,我寫我的應用程序如下:
checkNotNull(SERVER_URL, "SERVER_URL");
checkNotNull(SENDER_ID, "SENDER_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.
// NOT required any more GCMRegistrar.checkManifest(this);
/**
* this code to register reciver moved to message actvity
*/
//registerReceiver(mHandleMessageReceiver, new IntentFilter(
// DISPLAY_MESSAGE_ACTION));
/* final String */regId = GCMRegistrar.getRegistrationId(this);
/**
* save regId in pref to be used by Location update service
*/
SavePreferences("regId", regId);
if (regId.equals("")) {
// Automatically registers application on startup.
GCMRegistrar.register(this, SENDER_ID);
} else {
// Device is already registered on GCM, check server.
if (GCMRegistrar.isRegisteredOnServer(this)) {
;;
// Skips registration.
// -- mDisplay.append(getString(R.string.already_registered) +
// "\n");
// System.out.println(getString(R.string.already_registered)
// + "\n");
} 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) {
boolean registered = ServerUtilities.register(context,
regId);
// At this point all attempts to register with the app
// server failed, so we need to unregister the device
// from GCM - the app will try to register again when
// it is restarted. Note that GCM will send an
// unregistered callback upon completion, but
// GCMIntentService.onUnregistered() will ignore it.
if (!registered) {
GCMRegistrar.unregister(context);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}
};
mRegisterTask.execute(null, null, null);
}
}
編輯:請注意,我可以在設備上接收消息。
謝謝...這裏的訣竅我如何需要爲相同的設備生成一個新的ID,以便我可以刪除舊的ID? – user836026 2013-02-22 18:30:19
sry .. 當一個新的ID生成時,你會嘗試註冊它的GCM註冊商。 當這個新的註冊ID成功註冊您的onRegister()方法的服務將被調用.checked下面的代碼。 在這裏你可以添加代碼來替換以前的新代碼。 @Override \t保護無效onRegistered(上下文語境,字符串registrationId){ \t \t ServiceUtilities.register(上下文,registrationId); \t} – mahesh 2013-02-22 18:37:07
sry,但我不能正確縮進註釋部分上面的代碼,不能添加此代碼作爲另一個,因爲它反對stackoverflow規則。 從以下時間請問新問題作爲單獨的問題 – mahesh 2013-02-22 18:44:20