GCM Reg ID is empty

2019-02-25 00:53发布

问题:

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

回答1:

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;
        }
    ...
    }


回答2:

The first time is going to be null, if the registration is correct then the next time regid is not going to be null.

Remember to add at your GCMIntentService your SENDER_ID on the constructor

public GCMIntentService(){
    super(SENDER_ID);
}

Well you have defined it as PROJECT_ID, check if it is the number next to the url

https: //code.google.com/apis/console/#project:{SENDER_ID}

on your Google Api Project