kiran guled kiran guled - 2 months ago 9
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
return;
}//

// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);

// Make sure the manifest permissions was properly set
GCMRegistrar.checkManifest(this);

// 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.

Toast.makeText(getApplicationContext(),
regId + " - Already registered with GCM Server",
Toast.LENGTH_LONG).show();
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>() {

@Override
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
e.printStackTrace();
}

return null;
}

@Override
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() {

@Override
public void onReceive(Context context, Intent intent) {

String newMessage = intent.getExtras().getString(
Config.EXTRA_MESSAGE);

// Waking up mobile if it is sleeping
aServerUtility.acquireWakeLock(getApplicationContext());

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

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

// Releasing wake lock
aServerUtility.releaseWakeLock();
}
};

}

Koh Koh
Answer

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.

Comments