Why does GCM return an empty registration identifier for some Android devices?

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 successfully get the registration ID from GCM for some Android devices, but not for all Android devices. GCM returns an empty registration identifier for some Android devices. I use the GCM.jar file as a third-party tool that returns a registration identifier. All permissions are correctly defined in the manifest file. Sender ID is correct. The correct API key.

+4
source share
2 answers

I am not sure where exactly the empty registration identifier is returned, but it is important to understand that:

 GCMRegistrar.register(this, "517739810110"); 

is an asynchronous event and does not occur immediately. Therefore, the call:

 GCMRegistrar.getRegistrationId(this); 

right after you do not specify the registration identifier only an empty string. To get the registration ID correctly after your device has registered with the GCM servers, you need to process the following callback in your GCMBaseIntentService:

 protected void onRegistered(Context context, String registrationId) 

Here you will receive the registration ID and where you must register on the application server. You should use the GCM client sample as a guide for 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 keep track of how the GCM client sample is registered with DemoActivity :

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

Note that it does not request a registration identifier immediately after calling the register () method.

+6
source

Call this mathod two times, because several times on finished Android that does not receive this identifier; String identifier = 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; } 
0
source

All Articles