GCM Android中未生成应用程序注册ID

3

我曾尝试过浏览一些论坛页面和在stackoverflow上搜索相关的内容,但是没有找到答案。

我使用下面的代码为GCM通知生成 注册 ID。但是我得到了一个空字符串作为注册 ID:

GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this);

if (GCMRegistrar.isRegistered(this)) { Log.d("info", GCMRegistrar.getRegistrationId(this)); }

regId = GCMRegistrar.getRegistrationId(this);

Log.v("log", " register id oslo : " + regId);

if (regId.equals("")) {
    // replace this with the project ID
    GCMRegistrar.register(this, "718437041388");
    Log.d("info", GCMRegistrar.getRegistrationId(this));
} else {
    Log.d("info", "already registered as" + regId);
}

请帮我,如果我有任何错误,请告诉我。

提前感谢您, Nirav。


嗨,你解决了那个问题吗?我也遇到了同样的问题。 - Parvin Gasimzade
5个回答

7

GCMRegistrar.register(this, "718437041388");之后不能立即获取registrationId。
事实上,您是否有正确的GCMIntentService.java来接收注册ID?
如果是,请从GCMIntentService.java中的onRegistered中获取注册ID。

 @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        Log.d("GCMIntentService", "in GCMIntentService");
        displayMessage(context, getString(R.string.gcm_registered));
        ServerUtilities.register(context, registrationId);
    }

1
你好jjLin,感谢您的快速回复,但问题是在应用程序注册时出现,而不是接收消息时。当我们执行代码时,它应该将注册ID作为字符串获取,但我得到的是一个空字符串。如果您知道解决方案,请帮助我。谢谢。 - thedarkcoder
1
抱歉,为什么您认为问题出在注册时? - jjLin
实际上,我正在使用GCM创建一个通知应用程序,因此我需要该应用程序的注册ID。可以使用上述代码获取该ID,因为我们已经在另一个应用程序中测试和验证了该代码,并发现它可以正常工作。所以请告诉我问题出在哪里? - thedarkcoder
你应该将 Log.d("info", GCMRegistrar.getRegistrationId(this)); 放在 if else 之外。 - jjLin

5
请注意:
    GCMRegistrar.register(this, "...");

这是异步的。因此,在此之后立即调用GCMRegistrar.getRegistrationId(this)是不可靠的,因此您应该避免这样做。

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

请注意,GCMIntentService 应该被定义在您应用的根包中才能正常工作。


1

尝试使用带有Google API的模拟器,并与Google账户同步。


这并没有提供问题的答案。如果要批评或请求作者澄清,请在他们的帖子下留言 - 您始终可以在自己的帖子上发表评论,并且一旦您拥有足够的声望,您将能够评论任何帖子 - bmargulies
其实我也遇到了和他一样的问题,但是在这样做之后我成功注册了。 - Zubair

0
在你的项目的lib文件夹中添加GCM jar文件。

0

我在经过一天的努力后,终于找到了解决方案。我的问题是这样的:我把GCM相关的代码放到了我的主包中,并根据我的包名在androidManifest.xml文件中进行了更改。

我给你一个简单的例子。我的项目名称是GCMExample,我有三个包:

  1. com.examle.GCMExample(主包)
  2. com.examle.GCMExample.activity(第二个包)
  3. com.example.GCMExample.adapter(第三个包)

当我的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" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接