0

我正在将 GCM 实施到我的应用程序中。我使用这里的教程作为起点。

问题是我regId的总是null

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);

if (regId.equals(""))
{
    GCMRegistrar.register(this, "627xxxx78");
}
else
{
    if (GCMRegistrar.isRegisteredOnServer(this))
    {
        Log.i(TAG, "Already registered regId : " + regId );
    }
    else
    {
        final Context context = this;

        mRegisterTask = new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                boolean registered =
                        ServerUtilities.register(context, regId);

                if (!registered)
                {
                    GCMRegistrar.unregister(context);
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                mRegisterTask = null;
            }
        };

        mRegisterTask.execute(null, null, null);
    }
}

在上面的代码中,ServerUtilities.register永远不会调用,因为 GCMRegistrar.getRegistrationId(this); 总是返回一个空字符串。

有任何想法吗?

4

2 回答 2

2

确保您的模拟器或设备已使用 Google 帐户登录。我遇到了同样的问题。我只是登录了设备的谷歌帐户,大声笑,我得到了 regId。

于 2013-04-29T14:16:07.597 回答
2

我有同样的问题。在您的应用程序清单文件中添加以下内容:

         package="yourApplicationPackage"

      <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
      <uses-permission android:name="android.permission.WAKE_LOCK"/>
     <permission
        android:name="yourApplicationPackage.permission.C2D_MESSAGE"
       android:protectionLevel="signature" />
    <uses-permission
        android:name="yourApplicationPackage.permission.C2D_MESSAGE" />
  <!-- This app has permission to register and receive data message. -->
   <uses-permission
        android:name="com.google.android.c2dm.permission.RECEIVE" />

    <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="yourApplicationPackage" />
         </intent-filter>
    </receiver>
     <service android:name="yourApplicationPackage.GCMIntentService"/>

必须在应用程序包中保留此“yourApplicationPackage.GCMIntentService”

yourApplicationPackage.GCMIntentService 代码是这样的。

     public class GCMIntentService extends GCMBaseIntentService  {


@Override
protected void onError(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}

@Override
protected void onMessage(Context context, Intent intent) {
    // TODO Auto-generated method stub


 }

@Override
protected void onRegistered(Context context, String registrationId) {
    // TODO Auto-generated method stub
      Log.i(TAG, "Device registered: regId = " + registrationId);
     GcmServer.register(context, registrationId);
}

@Override
protected void onUnregistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub

}
于 2012-09-05T17:18:06.123 回答