This is really annoying me. I have an app that uses GCM. I have people complaining they can't login due to an error. Let me show you how I am making the Login:
// Login Activity
//....
public void onClickLoginButton (View v) {
// Do some stuff...
new GCMRegister().execute(); //AsyncTask
}
private class GCMRegister extends AsyncTask<Void, Void, Integer> {
@Override
protected Integer doInBackground (Void... params) {
ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
try {
//Get GCM Registration key
registrationId = googleCloud.register(Settings.PROJECT_NUMBER);
} catch (Exception e) {
Log.e("GCMRegister", e.toString());
return -1;
}
return 1;
} else {
return -3;
}
}
@Override
protected void onPostExecute(Integer result) {
if (result == 1) {
new LoginAsync().execute(); // Another AsyncTask to check in my database if user is valid and save its key
} else {
user.setEnabled(true);
password.setEnabled(true);
login.setEnabled(true);
if (result == -1) {
Toast.makeText(Login.this, "A problem occured login in", Toast.LENGTH_LONG).show();
} else if (result == -3) {
Toast.makeText(Login.this, "I need an internet connection", Toast.LENGTH_LONG).show();
}
}
}
So a lot of people is complaining they can't login in because of "A problem occured login" error, which indicates that GCM is failing on register. Even myself with my device, Android 4.4.2, can't do it at first. I need to try to login 2 or 3 times until it works (and Im in a good connection). The error on my LogCat is:
08-04 21:29:10.922: E/GCMRegister(18182): java.io.IOException: SERVICE_NOT_AVAILABLE
So what is wrong with my code? This is driving me nuts.
We faced the same issue, where users can't login the first time but can login after say the 2nd or 3rd time irrespective of the network speed.
We did a workaround by using Thread.sleep and retry for a number of times, till the GCM ID is recieved.
int noOfAttemptsAllowed = 5; // Number of Retries allowed
int noOfAttempts = 0; // Number of tries done
bool stopFetching = false; // Flag to denote if it has to be retried or not
String regId = "";
while (!stopFetching)
{
noOfAttempts ++;
GCMRegistrar.register(getApplicationContext(), "XXXX_SOME_KEY_XXXX");
try
{
// Leave some time here for the register to be
// registered before going to the next line
Thread.sleep(2000); // Set this timing based on trial.
} catch (InterruptedException e) {
e.printStackTrace();
}
try
{
// Get the registration ID
regId = GCMRegistrar.getRegistrationId(LoginActivity.this);
} catch (Exception e) {}
if (!regId.isEmpty() || noOfAttempts > noOfAttemptsAllowed)
{
// If registration ID obtained or No Of tries exceeded, stop fetching
stopFetching = true;
}
if (!regId.isEmpty())
{
// If registration ID Obtained, save to shared preferences
saveRegIDToSharedPreferences();
}
}
Note: The Thread.sleep and noOfAttemptsAllowed can be played around with based on your design and other parameters. We had a sleep time of 7000 so that probability of getting registered at first attempt is higher. However, if it fails, the next attempt would consume another 7000ms. This might cause users to think your app is slow. So, play around intelligently with those two values.
I have came across this same issue. My issue was that I was doing it on the onCreate method of my activity. What I discovered was that the context was not fully implemented causing the gcm to not successfully register. I provided getApplicationContext() for obtaining the GCM instance and this was able to resolve my issue.
Example:
Using context of activity caused an issue
GoogleCloudMessaging.getInstance(ActivityName.this)
Using application context resolved my issue
GoogleCloudMessaging.getInstance(getApplicationContext())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With