GCMRegistrar.getRegistrationId(this) always returns empty string, even after successful call to register

I am implementing GCM – the first thing I do is call GCMRegistrar.getRegistrationId to see if the device is registered. It’s not (empty string is returned), so I make the asynchronous call to GCMRegistrar.register. As expected, my intent service (derived from GCMBaseIntentService) gets its onRegistered() method called, along with a token that my server can use to send GCM messages to this device. So far, so good.

However, the next time I run the app, I would expect that first call to GCMRegistrar.getRegistrationId to return the token that I got back in my intent service’s onRegistered function. But I get the empty string again. I thought that the GCM client library (gcm.jar) would save the GCM registration id (token) to a private SharedPreferences object — in fact, the documentation for the GCMRegistrar class (http://developer.android.com/guide/google/gcm/client-javadoc/com/google/android/gcm/GCMRegistrar.html) states “Note: this class uses a private SharedPreferences object to keep track of the registration token”

  • Disable/Enable mobile data on Android L with root access
  • How does exactly custom Shadow objects work in Robolectric?
  • Recognize numbers using gestures
  • Phonegap Vs native iOS/Android development in 2014
  • How do I change the font color of the selection list in a spinner?
  • Crashlytics distribution for app with multiple flavors
  • I have not unregistered the device, uninstalled the application, or updated the versionCode. Any ideas what I am missing? Thanks

    Note — This question has been posted in google groups under the android-gcm tag. The url is here:

    https://groups.google.com/forum/#!topic/android-gcm/4FPqZLzlvE4

    Mark Murphy (a Commons Guy) has been real helpful with initial diagnosis of the problem but nothing concrete has surfaced

    Update Edit – Here is the relevant part of my AndroidManifest.xml

        <receiver android:name=".push.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="com.myapp.package.name" />
            </intent-filter>
        </receiver>
    
        <service android:name=".push.GCMIntentService" />
    

    Related posts:

    How I get Attribute using by XMLPull parser
    AppCompatEditText.getpParent() inside TextInputLayout returns FrameLayout
    How to get an Android widget&#039;s size after layout is calculated?
    How to get thumbnail for video in my /sdcard/Android/data/mypackage/files folder?
    How are existing VPN applications creating new profiles in Android 2.0 - 2.3?
    android:layout_alignParentBottom is ignored when used without explicit layout height as a row in Lis...
  • Detecting when user has dismissed the soft keyboard
  • Proguard error when exporting signed app using android.support.v4.app honeycomb compatibility package
  • Prevent Google play from auto translating my app description
  • Why android gives warning on using size smaller than 12sp?
  • Listview same height as content
  • Check user open the site in App or not
  • 7 Solutions collect form web for “GCMRegistrar.getRegistrationId(this) always returns empty string, even after successful call to register”

    *“category android:name=”write down your full package name” />”*

    give your package in manifest like above.

    Check if you have called GCMRegistrar.unregister(this); method somewhere that is called when app is closed. Maybe in onDestroy() ??
    In our case it was the problem 🙂

    The method actually un-registers the device from Google services, so it does not get a token until you open your app and get the register action called.

    Please check out the documentation for the sample app. In the manifest it says:

          By default, it must be named .GCMIntentService, unless the
          application uses a custom BroadcastReceiver that redefines its name.
    

    In your manifest you are using

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

    You are not placing this service on the root level and not making a custom BroadcastReceiver. This should be :

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

    I was having the same problem, moving the service to the root of my package solved the problem. This is kinda silly solution though.

    also add GCM permission into your manifest and verify google gmc key.

    App receives GCM messages.

    No worries, If getRegistrationId returns empty string, that is because some times it takes time to register to the GCM Cloud. You should wait for onRegistration method to trigger in the GCMIntent Service class.

    Once it is registered, you will receive the registrationID, then you can do whatever you want to with it(most likely you would have to send it to your web database, you can call Async from onRegistration callback method).

    @Override
    protected void onRegistered(Context arg0, String arg1) {

        //Log.i("Registration", arg1.toString());  
        // This is where you need to call your server to record the device registration id.
    }
    

    I double checked the AndroidManifest to ensure that our service was defined properly and it was. In the end I went with a hack that involved writing to shared preferences.

    put your service class which derived from GCMIntent service in the root package of your application. i mean if your application package is com.stackoverflow.android your service class should reside in this package not in one of the others package pakage of your application project. assure to put all required permission in the manifest file

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.