Android GCM : GCMRegistrar gives empty registratio

2019-01-15 10:27发布

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??

12条回答
女痞
2楼-- · 2019-01-15 11:02

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.
查看更多
姐就是有狂的资本
3楼-- · 2019-01-15 11:03

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.

查看更多
Melony?
4楼-- · 2019-01-15 11:05

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.

查看更多
叛逆
5楼-- · 2019-01-15 11:05

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

查看更多
神经病院院长
6楼-- · 2019-01-15 11:08

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.

查看更多
祖国的老花朵
7楼-- · 2019-01-15 11:11

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.

查看更多
登录 后发表回答