Alex Rivera | Logout

I can not get registration ID from Android GCM

Asked 2012-07-29T22:03:37.627
18

Although I try other answer about this problem, it can't be solved. Sender ID is correct, permissions are added, API key is true.

I use this post for creating the project: http://developer.android.com/guide/google/gcm/gs.html#server-app

    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
      GCMRegistrar.register(this, SENDER_ID);
      regId = GCMRegistrar.getRegistrationId(this);
    } else {
      Log.v(TAG, "Already registered");
    }
    String did = getDeviceID();

it returns empty string.

Edit
Report

2 Answers

11

If your application launches first time, you have to wait for OnRegistered callback on your GCMIntentService.java file.

GCMRegistrar.register(this, SENDER_ID);
// Android does NOT wait for registration ends and executes immediately line below
// So your regId will be empty.
regId = GCMRegistrar.getRegistrationId(this);

GCMIntentService.java:

@Override
protected void onRegistered(Context c, String regId) {
    // You can get it here!
}

Edit: Newer version of GCM library (which bundled with google play services library) waits for response and returns the Registration ID. So this answer is for older GCM libraries.

answered 2013-02-15T12:06:17.093
1

For example your receiver class name is : "MyCustomIntentService.java" , just change this name as "GCMIntentService.java" then try it again. This is simple solution. Just change your file name on the SRC area.

answered 2013-07-28T21:25:59.320

Your Answer