GCM Reg ID is empty

Hi I am trying to implement GCM push notification. I am getting an empty GCM id. Here is my code:

public void registerClient() {

        try {
        // Check that the device supports GCM (should be in a try / catch)
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        regId = GCMRegistrar.getRegistrationId(this);


        if (regId.equals("")) {
            registrationStatus = "Registering...";
            GCMRegistrar.register(this, PROJECT_ID);
            regId="sfsaas";
            regId = GCMRegistrar.getRegistrationId(this);
            registrationStatus = "Registration Acquired";
            Log.i("******IN_IF_REGID*****", regId);
            Log.i("INSIDE","if");
        } 
        else 
        {
            Log.i("INSIDE","else");
            registrationStatus = "Already registered";
            Log.i("******REGID*****", regId);
        }

        } 
        catch (Exception e) {
            e.printStackTrace();
            registrationStatus = e.getMessage();
        }

}

Here is my Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.eventnotifier"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<permission
    android:name="com.example.eventnotifier.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"     />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="com.example.eventnotifier.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

<!-- Camera permission -->
    <uses-feature android:name="android.hardware.camera" />
<!-- receives GCM messages -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!-- GCM connects to Google services -->
    <uses-permission android:name="android.permission.INTERNET" />

<!-- GCM requires a Google account -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <uses-permission android:name="android.permission.USE_CREDENTIALS" />

    <uses-permission android:name="android.permission.READ_OWNER_DATA" />

<!-- wake the processor if a GCM message is received -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:color/black" >

    <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="com.example.eventnotifier" />
        </intent-filter>
    </receiver>

    <service android:name=".GCMIntentService" />
        <activity
            android:name="com.example.eventnotifier.HomeActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
        </activity>

        <activity
            android:name="com.example.eventnotifier.RegisterActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
        </activity>

     <activity
        android:name="com.example.eventnotifier.DateActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
     </activity>

    <activity
        android:name="com.example.eventnotifier.DescriptionActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
     </activity>

     <activity
        android:name="com.example.eventnotifier.MyEventActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
     </activity>

     <activity
        android:name="com.example.eventnotifier.CreateEventActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
     </activity>

     <activity
        android:name="com.example.eventnotifier.LocationActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
     </activity>

     <activity
        android:name="com.example.eventnotifier.AddGuestActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
     </activity>

     <activity
        android:name="com.example.eventnotifier.ContactListActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
     </activity>

     <activity
        android:name="com.example.eventnotifier.ContactTest"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
     </activity>

     <activity
        android:name="com.example.eventnotifier.InvitedGuestsActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
     </activity>

     <activity
        android:name="com.example.eventnotifier.MyInvitationsActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
     </activity>

     <activity
        android:name="com.example.eventnotifier.UnregisteredGuestsActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
     </activity>

     <activity
        android:name="com.example.eventnotifier.RecentGuestsActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">

     </activity>
 </application>  
</manifest>

This line is not getting printed -Log.i("**IN_IF_REGID***", regId); Can you guys help me out? I have to submit this project tomorrow. Any help will be appreciated! Thank you

Answers


It doesn't work like that.

GCMRegistrar.register initiates a request to GCM server, asking it for a Registration ID. This is an asynch request. GCMRegistrar.getRegistrationId returns a cached value of the Registration ID, so if your device is not registered, it returns null.

The Registration ID is returned in the GCMIntentService.onRegistered method.

Here's the code of GCMIntentService from the GCM Demo.

This is the code of GCMRegistrar.getRegistrationId :

/**
 * Gets the current registration id for application on GCM service.
 * <p>
 * If result is empty, the registration has failed.
 *
 * @return registration id, or empty string if the registration is not
 *         complete.
 */
public static String getRegistrationId(Context context) {
    final SharedPreferences prefs = getGCMPreferences(context);
    String registrationId = prefs.getString(PROPERTY_REG_ID, "");
    // check if app was updated; if so, it must clear registration id to
    // avoid a race condition if GCM sends a message
    int oldVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
    int newVersion = getAppVersion(context);
    if (oldVersion != Integer.MIN_VALUE && oldVersion != newVersion) {
        Log.v(TAG, "App version changed from " + oldVersion + " to " +
                newVersion + "; resetting registration id");
        clearRegistrationId(context);
        registrationId = "";
    }
    return registrationId;
}

As you can see, it returns a locally stored value of the Registration ID. In order for it to return anything, GCMRegistrar.setRegistrationId(Context context, String regId) must be called, with the Registration ID that was returned to GCMIntentService. And it's called in GCMBaseIntentService :

    private void handleRegistration(final Context context, Intent intent) {
        GCMRegistrar.cancelAppPendingIntent();
        String registrationId = intent.getStringExtra(EXTRA_REGISTRATION_ID);
        String error = intent.getStringExtra(EXTRA_ERROR);
        String unregistered = intent.getStringExtra(EXTRA_UNREGISTERED);
        Log.d(TAG, "handleRegistration: registrationId = " + registrationId +
                ", error = " + error + ", unregistered = " + unregistered);

        // registration succeeded
        if (registrationId != null) {
            GCMRegistrar.resetBackoff(context);
            GCMRegistrar.setRegistrationId(context, registrationId);
            onRegistered(context, registrationId);
            return;
        }
    ...
    }

Need Your Help

How to fix routing with pagination and long variable query strings

symfony1 symfony-1.4

I have a problem going from page one to page two, three, four, etc... when using pagination along with a long and variable query string. I suspect its a routing issue. I’m using a custom query th...

how to get the name of a button created by QDialogButtonBox?

c++ qt button widget

I'm trying get all the button child widgets of a window. The buttons were created through QDialogButtonBox. How do I get the which one is the cancel/ok/save button?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.