4

I have followed http://developer.android.com/guide/google/gcm/gs.html#server-app to implement GCM in my application

                    GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        if (GCMRegistrar.isRegistered(this)) {
            Log.d(TAG, GCMRegistrar.getRegistrationId(this));
        }
        final String regId = GCMRegistrar.getRegistrationId(this);

        if (regId.equals("")) {
            GCMRegistrar.register(this, sender_id);
            Log.d(TAG, "Registration id :  "+GCMRegistrar.getRegistrationId(this));
        }
            else {
            Log.d("info", "already registered as" + regId);
        }

that returns empty string as registration ID what else is needed to get the registration ID??

4

12 回答 12

10

Empty String comes when device is not registered successfully. There may be following reasons for it-

  1. Put you GCM code in application package. [You can make another package with same name of application package]
  2. Put all permissions properly.
于 2012-08-13T12:42:08.040 回答
3

I also got the empty registration ID and finally fix it. Here are my solutions:

1) check the project ID. It should be the 12 char long shown in the hyperlink of Google API's console (not the Project ID you named in the project summary)
2) try change the final String regId to String regId. I don't know why but it works for me.

Hope it helps.

于 2012-07-15T04:47:47.470 回答
1
    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    final String regId = GCMRegistrar.getRegistrationId(this);

    if (regId.equals("")) {
        GCMRegistrar.register(this, "YOUR_ACCOUNT");
    } else {
        app.prefs.edit().putString("prefs_googleid", regId).commit();
        GCMRegistrar.setRegisteredOnServer(this, true);
        Log.v(TAG, "Already registered");
    }

then wait the call back in your receiver that extend GCMBaseIntentService in the onRegistered ovverride, there you will get your ID registration.

Anyway I full create an app that use GCM following this post: http://developer.android.com/guide/google/gcm/gs.html

于 2012-07-05T09:45:54.317 回答
1

I got the same issue, the solution was:

You need to implement a GCMIntentService (extending from GCMBaseIntentService). And DO NOT !!! rename GCMIntentService by something else, this was a reason in my case.

于 2012-09-09T16:16:21.953 回答
1

Creating a new project using google console with the same name as application's name in eclipse helped me to solve this problem.

于 2013-01-27T05:59:58.977 回答
0

I also got the empty string as a registration id and solved it as follow :

  1. Check whether senderId is correct or not.
  2. Check whether there is an active google account signed in your device or not.
  3. Check wheter permissions are correctly defined in manifest file or not.
于 2012-07-19T08:59:00.573 回答
0

Make sure you defined the service in the "app_package" as this intent service will be called by the GCMBroadcastReceiver

For example

<service android:name=".GCMIntentService" />

or

<service android:name="app_package.GCMIntentService" />

Fail to define the service correctly, there will be no callback for onRegister, and your Register Id is always empty

于 2013-05-09T06:30:46.010 回答
0

I had the same problem with my application. First thing that was wrong, was with my GCMIntentService. It has to be at the root of your package.

I will include here a short snippet of my GCMIntentService. Please keep in mind that, you will receive your registrationID on GCMIntentService register method

public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super(CloudMessagingUtility.SENDER_ID);
}

/**
 * Method called on device registered
 **/
@Override
protected void onRegistered(Context context, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);

//here call your methods to send the registrationId to your webserver CloudMessagingUtility.register(context, registrationId); }

/**
 * Method called on device un registred
 * */
@Override
protected void onUnregistered(Context context, String registrationId) {
    Log.i(TAG, "Device will unregister from Google Cloud Messaging");
    //unregister the device from your own webserver too
    CloudMessagingUtility.unregister(context, registrationId);
}

/**
 * Method called on Receiving a new message
 * */
@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message from GCM:");
    String message = "";//TODO handle here the messages!

}

/**
 * Method called on receiving a deleted message
 * */
@Override
protected void onDeletedMessages(Context context, int total) {
    Log.i(TAG, "Received deleted messages notification");
    String message = getString(R.string.gcm_deleted, total);
    displayMessage(context, message);

}

/**
 * Method called on Error
 * */
@Override
public void onError(Context context, String errorId) {
    Log.e(TAG, "Received error: " + errorId);
}

}

you have to make the right configuration in the Android

e <service android:name=".GCMIntentService" /><receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >

        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name=" your package here" />
        </intent-filter>
    </receiver>
于 2013-06-07T22:50:10.063 回答
0

GCMRegistrar.register(this, sender_id); Log.d(TAG, "Registration id : "+GCMRegistrar.getRegistrationId(this));

Calling getRegistrationId() straight after register() is risky because register might not return the id straight away.

For my case I did a check later on in my script (upon button press) using:

boolean registered = GCMRegistrar.isRegistered(this);

if registered then call GCMRegistrar.getRegistrationId(this)); to get the ID registered.

于 2013-08-02T05:29:58.407 回答
0

I have tried the second solution of Nick Wong and i changed => public static final String PROPERTY_REG_ID = "registration_id"; to => public static String PROPERTY_REG_ID = "registration_id";

and simply it worked.

于 2013-12-12T21:07:08.137 回答
0

Since GCMRegistrar.getRegistrationId is asynchronous, I added a counter to check whether regId is actually empty or not. If empty the counter would just increment and try to get the registration Id again. A fairly large number like 100 should suffice.

int counter = 0;
                while (true) {
                    regID = GCMRegistrar.getRegistrationId(this);
                    counter++;
                    if (counter == 100 || regID != null || !regID.equals("")) {
                        break;
                    }
                }
于 2014-09-25T11:15:35.250 回答
0

I've solved changing the project used for registering with GCMRegistrar.register from the project name to the google project number

于 2015-06-05T17:20:04.263 回答