Android GCM注册表ID为空

Android GCM注册表ID为空,android,push-notification,google-cloud-messaging,Android,Push Notification,Google Cloud Messaging,嗨,我正在尝试实现GCM推送通知。我得到一个空的GCM id。 这是我的密码: public void registerClient() { try { // Check that the device supports GCM (should be in a try / catch) GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this);

嗨,我正在尝试实现GCM推送通知。我得到一个空的GCM id。 这是我的密码:

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();
        }

}
这是我的舱单:

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

这一行没有打印-Log.i(“**IN\u IF\u REGID***”,REGID);你们能帮帮我吗? 我明天必须提交这个项目。任何帮助都将不胜感激!
谢谢

它不是那样工作的

gcmregistar.register
向GCM服务器发起请求,要求其提供注册ID。这是一个异步请求
gcmregistar.getRegistrationId
返回注册ID的缓存值,因此如果您的设备未注册,它将返回null

注册ID在
gcminentservice.onRegistered
方法中返回

以下是GCM演示中的代码

这是gcmregistar.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;
}

第一次将为null,如果注册正确,则下一次regid不会为null

请记住在GCMinentService中在构造函数上添加发件人ID

public GCMIntentService(){
    super(SENDER_ID);
}
您已经将其定义为PROJECT_ID,请检查它是否是url旁边的数字

https://code.google.com/api/console/#项目:{SENDER_ID}


在您的Google Api项目上

那么我应该在gcminentservice.onRegistered方法中收集regId吗?如果您调用
GCMRegistrar.register
,那么您应该从
onRegistered
方法中收集regId并将其传递给您的服务器。如果设备已经注册,您不必调用register,在这种情况下,您可以从gcmregistar获取regID(尽管在这种情况下,您的服务器应该已经拥有注册ID,所以我不明白您为什么需要获取它)。您可以调用
gcmregistar.isRegistered
,以了解您是否已经注册,并基于此决定是否调用
register
。我是否可以在我的主要活动中获得注册码以及服务?因为我正在从Main Activity将数据插入我的datadabase Hey Eran感谢您的帮助。已成功将regid发送到服务器!!:)我正在gcminentservice onRegistered()中获取regID。是否有一种方法可以将数据发送到我的活动?
public GCMIntentService(){
    super(SENDER_ID);
}