18

尽管我尝试了有关此问题的其他答案,但无法解决。发件人 ID 正确,添加权限,API 密钥为真。

我使用这篇文章来创建项目:http: //developer.android.com/guide/google/gcm/gs.html#server-app

    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
      GCMRegistrar.register(this, SENDER_ID);
      regId = GCMRegistrar.getRegistrationId(this);
    } else {
      Log.v(TAG, "Already registered");
    }
    String did = getDeviceID();

它返回空字符串。

4

6 回答 6

49

您是否已在应用程序的根包中正确GCMIntentService定义?

<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="my_app_package" />
  </intent-filter>
</receiver>

确保它"my_app_package"与您的应用程序的主包相同,否则您的 id 将返回一个空字符串。

还要注意

    GCMRegistrar.register(this, "...");

是异步的。因此,在那之后立即调用GCMRegistrar.getRegistrationId(this)是不可靠的,所以你应该避免它。

GCMIntentService作为注册过程的一部分,该 ID 将通过广播回调到达您的 . 然后,您可以从那里将 gcm/fcm 密钥存储在您喜欢的任何地方,并在应用程序的其他部分(通常在服务器上)使用它。

于 2012-07-31T08:34:11.483 回答
11

如果您的应用程序第一次启动,您必须等待 GCMIntentService.java 文件上的 OnRegistered 回调。

GCMRegistrar.register(this, SENDER_ID);
// Android does NOT wait for registration ends and executes immediately line below
// So your regId will be empty.
regId = GCMRegistrar.getRegistrationId(this);

GCMIntentService.java:

@Override
protected void onRegistered(Context c, String regId) {
    // You can get it here!
}

编辑:较新版本的 GCM 库(与 google play 服务库捆绑)等待响应并返回注册 ID。所以这个答案适用于较旧的 GCM 库。

于 2013-02-15T12:06:17.093 回答
5

经过1天的努力,我遇到了同样的问题,我找到了解决方案。首先,我将我的 GCM 相关代码放入我的主包中,并根据 androidManifest.xml 中的我的包进行更改

我给你一个简单的例子。我的项目名称是 GCMExample,我有三个包 1. com.examle.GCMExample(Main Package) , 2. com.examle.GCMExample.activity(Second Package) , 3. com.example.GCMExample.adapter(Third Package)

当我的 GCM 相关类文件进入我的第二个包时,我没有获得注册 ID

我的 GCM 相关类,如 1. GCMIntentService 、 2. ServerUtilities 、 3. CommonUtilities 4. WakeLocker 放入我的主包 com.example.GCMExample

还有我的 androidManifest.xml

     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />
     <uses-permission android:name="android.permission.WAKE_LOCK" />
     <uses-permission android:name="android.permission.VIBRATE" />

     <permission android:name="com.example.GCMExample.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

     <uses-permission android:name="com.example.GCMExample.C2D_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="com.example.GCMExample" />
      </intent-filter>
</receiver>  

<service android:name="com.example.GCMExample.GCMIntentService" />
于 2014-02-20T05:48:33.360 回答
2

注册发件人“my_sender_id”的应用程序 com.example.ilkgcm - 此错误表明您尚未将“my_sender_id”更改为有效的发件人 ID,该 ID 为 12 个字母代码。

于 2012-10-02T09:51:08.967 回答
1

如果您没有从注册中得到响应。主要原因之一是您的清单文件配置不正确......尤其是正确地给出“package_name”(您的应用程序包名称,如 com.xxx.yyy)。

于 2013-06-28T22:49:45.607 回答
1

例如,您的接收器类名称是:“MyCustomIntentService.java”,只需将此名称更改为“GCMIntentService.java”,然后再试一次。这是一个简单的解决方案。只需在 SRC 区域更改您的文件名。

于 2013-07-28T21:25:59.320 回答