Gcm Registering Twice With 2 Different Regids
in my app I have this code to register to the GCM service: Server.java protected void registerToGcm() { GCMRegistrar.checkDevice(mContext); GCMRegistrar.checkManifest(mCont
Solution 1:
What do you need to check when you want to register is
GCMRegistrar.checkDevice(getApplicationContext());
GCMRegistrar.checkManifest(getApplicationContext());
deviceToken = GCMRegistrar.getRegistrationId(getApplicationContext());
if (printLog == true)
Log.i("Home", "Registration id ===== " + deviceToken);
if (deviceToken.equals("")) {
GCMRegistrar.register(getApplicationContext(), SENDER_ID);
} else {
if (printLog == true)
Log.v("Home", "Device Already registered");
}
Solution 2:
you Should Go with Below Code to Ensure you are register the RegId to Server only if it is not registerd to Server.
publicvoidGCMWebService(final String regId) {
if (regId.equals("")) {
Log.i(TAG,
"================Inside if in regId=null GCMWebService==============================");
// Automatically registers application on startup.
GCMRegistrar.register(mContext, GCMIntentService.SENDER_ID);
// GCMRegistrar.unregister(this);
} else {
Log.i(TAG,
"================Inside else in regId=null GCMWebService==============================");
// Device is already registered on GCM, needs to check if it is// registered on our server as well.if (GCMRegistrar.isRegisteredOnServer(this)) {
// Skips registration.
Log.i(TAG,
"================Inside else in regId=null Already register on Server GCMWebService=============================");
// mDisplay.append(getString(R.string.already_registered) +// "\n");
} else {
Log.i(TAG,
"================Inside else in regId=null trying to register on Server GCMWebService=============================");
// 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.finalContextcontext=this;
mRegisterTask = newAsyncTask<Void, Void, Void>() {
@Overrideprotected Void doInBackground(Void... params) {
Log.i(TAG,
"================Inside doInBackground Method GCMWebService==============================");
registerToServer(regId);
Log.i(TAG,
"================Server side REegistered or not?"
+ registered);
// 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) {
Log.i(TAG,
"================Inside unregister inside Do in backgroound GCMWebService==============================");
GCMRegistrar.unregister(context);
}
returnnull;
}
@OverrideprotectedvoidonPostExecute(Void result) {
Log.i(TAG,
"================Inside onPostExecute Method GCMWebService==============================");
mRegisterTask = null;
if (!registered) {
/*
* final Toast tag = Toast .makeText( context,
* "Please Check Your Internet Connection and Try again \nOr\nWebservice is not Working now..."
* , Toast.LENGTH_SHORT);
*/newCountDownTimer(8000, 1000) {
publicvoidonTick(long millisUntilFinished) {
Toast.makeText(
context,
"Please Check Your Internet Connection and Try again \nOr\nWebservice is not Working now...",
Toast.LENGTH_SHORT).show();
}
publicvoidonFinish() {
}
}.start();
}
}
};
mRegisterTask.execute(null, null, null);
}
}
}
Hope it Will Help.
Post a Comment for "Gcm Registering Twice With 2 Different Regids"