0

I'm trying to get GCM device registration id using Implementing GCM Client. I used a class Functions to implement the process. Here is my class

public class Functions {


public Functions(Activity activity) {
    // TODO Auto-generated constructor stub
    this.activity = activity;
    this.prefs = activity.getSharedPreferences(activity.getClass().getSimpleName(), Context.MODE_PRIVATE);
}

public void deviceRegistrationID(){
    if (checkPlayServices()) {
        gcm = GoogleCloudMessaging.getInstance(activity);
        regid = getRegistrationId(activity);

        if (regid.isEmpty()) {
            registerInBackground();
        }

    } else {
        Log.i(TAG, "No valid Google Play Services APK found.");
    }

}

public String getGCMRegistrationID(){
    String registeredID = prefs.getString(PROPERTY_REG_ID, ""); 
    System.out.println("The REG ID:"+prefs.getString(PROPERTY_REG_ID, ""));
    return registeredID;
}

private boolean checkPlayServices() {
    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(activity);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            GooglePlayServicesUtil.getErrorDialog(resultCode, activity,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        } else {
            activity.finish();
        }
        return false;
    }
    return true;
}

private String getRegistrationId(Context context) {
    String registrationId = prefs.getString(PROPERTY_REG_ID, "");
    if (registrationId.isEmpty()) {
        Log.i(TAG, "Registration not found.");
        return "";
    }
    int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION,
            Integer.MIN_VALUE);
    int currentVersion = getAppVersion(context);
    if (registeredVersion != currentVersion) {
        Log.i(TAG, "App version changed.");
        return "";
    }
    return registrationId;
}

private static int getAppVersion(Context context) {
    try {
        PackageInfo packageInfo = context.getPackageManager()
                .getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionCode;
    } catch (NameNotFoundException e) {
        // should never happen
        throw new RuntimeException("Could not get package name: " + e);
    }
}

private void registerInBackground() {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(activity);
                }
                regid = gcm.register(SENDER_ID);
                msg = "Device registered, registration ID=" + regid;
                storeRegistrationId(activity, regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
            }
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
            Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
        }
    }.execute(null, null, null);
}

private void storeRegistrationId(Context context, String regId) {
    int appVersion = getAppVersion(context);
    Editor editor = prefs.edit();
    editor.putString(PROPERTY_REG_ID, regId);
    editor.putInt(PROPERTY_APP_VERSION, appVersion);
    editor.commit();
}}

In my MainActivity i'm trying to get registration id in following way

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    functions = new Functions(MainActivity.this);
    functions.deviceRegistrationID();
    registrationId = functions.getGCMRegistrationID();}

Here registrationId generates null value. But in class Functions method

private void registerInBackground() {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            .......
        }

        @Override
        protected void onPostExecute(String msg) {
            Toast.makeText(activity, msg, Toast.LENGTH_LONG).show();
        }
    }.execute(null, null, null);
}

it shows registration id in Toast and in

public String getGCMRegistrationID(){
    String registeredID = prefs.getString(PROPERTY_REG_ID, ""); 
    System.out.println("The REG ID:"+prefs.getString(PROPERTY_REG_ID, ""));
    return registeredID;
}

it shows empty registration id in LogCat. How can i get the registration id?

user3384985
  • 2,975
  • 6
  • 26
  • 42

1 Answers1

0

this line of code can help you out for your question

private String getRegistrationId(Context context) {

 final SharedPreferences prefs = getGCMPreferences(context);

 String registrationId = prefs.getString(PROPERTY_REG_ID, "");

 if (registrationId.length() == 0) {

   Log.v(TAG, "Registration not found.");

  return "";

 }

 // check if app was updated; if so, it must clear registration id to

// avoid a race condition if GCM sends a message

int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);

 int currentVersion = getAppVersion(context);

  if (registeredVersion != currentVersion || isRegistrationExpired()) {

Log.v(TAG, "App version changed or registration expired.");

 return "";



return registrationId;

}

or as a reference you can check this link Handling registration ID changes in Google Cloud Messaging on Android

Community
  • 1
  • 1
Fatti Khan
  • 1,543
  • 1
  • 11
  • 19