4
votes
private void registerClient()    
{            
   try {  
            GCMRegistrar.checkDevice(this);  
            GCMRegistrar.checkManifest(this);  
            regId = GCMRegistrar.getRegistrationId(this);  
            if (regId.equals(""))   
            {  
              registrationStatus = "Registering...";                  
              GCMRegistrar.register(this, "517739810110");  
              regId = GCMRegistrar.getRegistrationId(this);
              registrationStatus = "Registration Acquired";  
              sendRegistrationToServer();
            } 
            else 
            {
              registrationStatus = "Already registered";
            }
     } 
  catch (Exception e) 
     {
           e.printStackTrace();
           registrationStatus = e.getMessage();
     }
}

I can obtain the registration id from GCM successfully for some Android devices but not for all Android devices. The GCM returns empty registration id for some Android devices. I am using the GCM.jar file as a third party tool which returns the registration id. All the permissions are correctly defined in the manifest file. The sender id is correct. The API key is correct.

2

2 Answers

6
votes

I'm not sure exactly where a blank registration ID is being returned, but it's important to realize that:

GCMRegistrar.register(this, "517739810110");  

is an asynchronous event and does not happen right away. So calling:

GCMRegistrar.getRegistrationId(this);

right after will not give you the registration ID only a blank string. In order to properly get the registration ID after your device has registered with the GCM servers you need to handle the following callback in your GCMBaseIntentService:

protected void onRegistered(Context context, String registrationId)

That is where you will get the registration ID and where you should register with your application server. You should use the GCM client sample as your guide for how to do this:

@Override
protected void onRegistered(Context context, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
    displayMessage(context, getString(R.string.gcm_registered,
            registrationId));
    ServerUtilities.register(context, registrationId);
}

You should also follow how the GCM client sample registers in it's DemoActivity:

final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
    // Automatically registers application on startup.
    GCMRegistrar.register(this, SENDER_ID);
}

Notice that it does not query for the registration ID right after calling the register() method.

0
votes

Call this mathod two times because some times on ready Android not getting that ID String Id = Notification();

if(Id.equals("")){
String Id =  Notification();
}        

  private String Notification() {
                registerReceiver(mHandleMessageReceiver, new IntentFilter(
                        DISPLAY_MESSAGE_ACTION));
                final String regId = GCMRegistrar
                        .getRegistrationId(ActivateScreen.this);

                // Check if regid already presents
                if (regId.equals("")) {
                    // Registration is not present, register now with GCM
                    GCMRegistrar.register(ActivateScreen.this, SENDER_ID);
                } else {
                    // Device is already registered on GCM
                    if (GCMRegistrar.isRegisteredOnServer(ActivateScreen.this)) {
                        // Skips registration.
                        // Toast.makeText(getApplicationContext(),
                        // "Already registered with GCM", Toast.LENGTH_LONG)
                        // .show();
                    } else {
                        final Context context = ActivateScreen.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);
                    }
                }

                Log.i("Device Id", appId);
                return regId;
            }