Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCM Register SERVICE_NOT_AVAILABLE

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.

like image 461
João Menighin Avatar asked Aug 05 '14 00:08

João Menighin


2 Answers

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.

like image 100
Nishanthi Grashia Avatar answered Oct 12 '22 02:10

Nishanthi Grashia


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())
like image 26
shug Avatar answered Oct 12 '22 02:10

shug