kiran guled kiran guled - 1 year ago 57
Android Question

How to register to GCM and Post to our Server at single attempt?

I am successful in registering to GCM server and posting registration id to our server but it is taking two attempts to complete the whole cycle. I need to do all these in single attempt. I am adding my code below please correct me if I am wrong.
First attempt: It will register to gcm server.
Second attempt : It will register to our server

protected void RegisterApp() {
// -----------------------------------
aServerUtility = (GCMServerController) getApplicationContext();

// Check if Internet present
if (!aServerUtility.isConnectingToInternet()) {

// Internet Connection is not present
aServerUtility.showAlertDialog(this, "Internet Connection Error",
"Please connect to Internet connection", false);
// stop executing code by return

// Make sure the device has the proper dependencies.

// Make sure the manifest permissions was properly set

// Register custom Broadcast receiver to show messages on activity
registerReceiver(mHandleMessageReceiver, new IntentFilter(Config.DISPLAY_MESSAGE_ACTION));

// Get GCM registration id
final String regId = GCMRegistrar.getRegistrationId(this);
final String Username = etUserName.getText().toString();

if (regId.equals("")) {

// Register with GCM
GCMRegistrar.register(common_signin.this, Config.GOOGLE_SENDER_ID);

} else {

// Device is already registered on GCM Server
if (GCMRegistrar.isRegisteredOnServer(this)) {

// Skips registration.

regId + " - Already registered with GCM Server",
Log.i(TAG, "Registered device (regId = " + regId + ")");

} 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>() {

protected Void doInBackground(Void... params) {

// Register on our server
// On server creates a new user
try {
aServerUtility.register(context, Username, "", regId);
} catch (JSONException e) {
// TODO Auto-generated catch block

return null;

protected void onPostExecute(Void result) {
mRegisterTask = null;


// execute AsyncTask
mRegisterTask.execute(null, null, null);


// ---------------------------------------

// Create a broadcast receiver to get message and show on screen
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

String newMessage = intent.getExtras().getString(

// Waking up mobile if it is sleeping

// Display message on the screen
// lblMessage.append(newMessage + "");

"Got Message: " + newMessage, Toast.LENGTH_LONG).show();

// Releasing wake lock


Koh Koh
Answer Source

Two calls to this method were needed because you only called GCMRegistrar.register() when there is no existing registration ID (in if (regId.equals(""))). The method ends after that and does not call aServerUtility.register(), which performs the server registration. It is only in the second call, when if (regId.equals("")) is false, that you actually make the call to register with your server.

Having said all this, please take note that GCMRegistrar is deprecated. I would suggest moving to GCM.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download