2012-07-05 56 views
4

我按照http://developer.android.com/guide/google/gcm/gs.html#server-app在我的应用程序的Android GCM:GCMRegistrar给空注册ID

    GCMRegistrar.checkDevice(this); 
     GCMRegistrar.checkManifest(this); 
     if (GCMRegistrar.isRegistered(this)) { 
      Log.d(TAG, GCMRegistrar.getRegistrationId(this)); 
     } 
     final String regId = GCMRegistrar.getRegistrationId(this); 

     if (regId.equals("")) { 
      GCMRegistrar.register(this, sender_id); 
      Log.d(TAG, "Registration id : "+GCMRegistrar.getRegistrationId(this)); 
     } 
      else { 
      Log.d("info", "already registered as" + regId); 
     } 

返回所需其他资料,以获得注册ID空字符串作为注册ID 实现GCM?

回答

1

在eclipse中使用与应用程序名称相同的名称使用谷歌控制台创建一个新项目帮助我解决了这个问题。

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

    if (regId.equals("")) { 
     GCMRegistrar.register(this, "YOUR_ACCOUNT"); 
    } else { 
     app.prefs.edit().putString("prefs_googleid", regId).commit(); 
     GCMRegistrar.setRegisteredOnServer(this, true); 
     Log.v(TAG, "Already registered"); 
    } 

然后等待您的接收器中的回调函数在onRegistered ovverride中扩展GCMBaseIntentService,那么您将获得您的ID注册。

无论如何,我完全创建使用GCM下面这篇文章的应用程序:http://developer.android.com/guide/google/gcm/gs.html

+0

感谢朱塞佩,我已经使用了相同的链接 但我的申请没有得到onRegistered()方法 – techieWings 2012-07-05 10:15:13

+0

您是否实现了BrodcastReceiver? – Giuseppe 2012-07-06 16:05:03

3

我也得到了空注册ID并最终解决它。这里是我的解决方案:

1)检查项目ID。它应该是12字符在谷歌API控制台的超级链接(并非项目ID在项目总结命名)
2所示长)尝试改变最后弦乐REGID字符串的RegID。我不知道为什么,但它适用于我。

希望它有帮助。

+0

感谢尼克,但这不是我的情况。 – techieWings 2013-01-27 05:57:23

+0

从注册ID变量中删除'final'解决了我的问题。谢谢! – srgtuszy 2013-04-08 19:26:10

+1

我滥用了项目ID和服务器密钥。感谢第一个解决方案。 – Youngjae 2013-05-18 08:06:58

0

我也得到了空字符串作为注册ID,并解决了这个问题如下:

  1. 检查senderId是否正确与否。
  2. 检查您的设备中是否有签署的有效Google帐户。
  3. 检查清单文件中是否正确定义了许可权限。
+0

thneks的地址栏URL中找到projectID,但即使这样做是正确的 – techieWings 2013-01-27 05:56:24

10

设备未成功注册时出现空字符串。可能有以下原因 它 -

  1. 把GCM代码放在应用程序包中。 [您可以制作另一个同名应用程序包]
  2. 正确设置所有权限。
+2

谢谢,但这是正确的。 – techieWings 2013-01-27 05:55:45

+2

直到我发现GCM代码必须位于应用程序包中时才知道。 – howettl 2013-07-19 06:53:01

+4

是的你是对的。 GCM代码必须在主包中。然后只有它工作。 – 2014-02-23 05:39:35

1

我得到了同样的问题,解决办法是:

你需要实现一个GCMIntentService(从GCMBaseIntentService延伸)。 And 不要!重命名GCMIntentService由别的东西,这是我的情况的原因。

+0

我已经这样做了.. – techieWings 2013-01-27 05:57:01

+0

-1因为'GCMIntentService'只是默认名称(预计在主包中)但是只要你正确地完成了任务,你可以使用'getGCMIntentServiceClassName()'正确覆盖 – 2014-06-05 20:14:45

0

确保您在“app_package”中定义的服务,因为这意图服务将由GCMBroadcastReceiver被称为

例如

<service android:name=".GCMIntentService" /> 

<service android:name="app_package.GCMIntentService" /> 

失败来定义服务正确,onRegister将不会回调,并且您的注册ID始终为空

0

我和我的应用程序有同样的问题。 错误的第一件事,是我的GCMIntentService。它必须在你的软件包的根基上。

我将在这里包含一个我的GCMIntentService的简短片段。请记住,您将收到您registrationID上GCMIntentService注册方法

public class GCMIntentService extends GCMBaseIntentService { 

private static final String TAG = "GCMIntentService"; 

public GCMIntentService() { 
    super(CloudMessagingUtility.SENDER_ID); 
} 

/** 
* Method called on device registered 
**/ 
@Override 
protected void onRegistered(Context context, String registrationId) { 
    Log.i(TAG, "Device registered: regId = " + registrationId); 

//这里调用你的方法到registrationId发送到Web服务器 CloudMessagingUtility.register(背景下,registrationId); }

/** 
* Method called on device un registred 
* */ 
@Override 
protected void onUnregistered(Context context, String registrationId) { 
    Log.i(TAG, "Device will unregister from Google Cloud Messaging"); 
    //unregister the device from your own webserver too 
    CloudMessagingUtility.unregister(context, registrationId); 
} 

/** 
* Method called on Receiving a new message 
* */ 
@Override 
protected void onMessage(Context context, Intent intent) { 
    Log.i(TAG, "Received message from GCM:"); 
    String message = "";//TODO handle here the messages! 

} 

/** 
* Method called on receiving a deleted message 
* */ 
@Override 
protected void onDeletedMessages(Context context, int total) { 
    Log.i(TAG, "Received deleted messages notification"); 
    String message = getString(R.string.gcm_deleted, total); 
    displayMessage(context, message); 

} 

/** 
* Method called on Error 
* */ 
@Override 
public void onError(Context context, String errorId) { 
    Log.e(TAG, "Received error: " + errorId); 
} 

}

你必须做出正确的配置在Android

e <service android:name=".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=" your package here" /> 
     </intent-filter> 
    </receiver> 
0

GCMRegistrar.register(这一点,SENDER_ID); Log.d(TAG,“Registration id: ”+ GCMRegistrar.getRegistrationId(this));

在register()之后直接调用getRegistrationId()是有风险的,因为注册可能不会立即返回id。

对于我来说,我在我的脚本(在按下按钮)使用做了检查以后:

布尔注册= GCMRegistrar.isRegistered(本);

if registered然后调用GCMRegistrar.getRegistrationId(this));获得注册的ID。

0

我试图尼克黄的第二溶液和我改变=> public static final String PROPERTY_REG_ID = "registration_id"; 到=> public static String PROPERTY_REG_ID = "registration_id";

和简单地它的工作。

+0

那很棒!对我来说,我已经实现了所有其他的事情,包括正确的,但我的包名称有错字。 – techieWings 2013-12-13 04:39:29

0

由于GCMRegistrar.getRegistrationId是异步的,我添加了一个计数器来检查regId是否实际为空。如果为空,则计数器只会递增并尝试再次获取注册标识。像100这样的相当大的数字应该就足够了。

int counter = 0; 
       while (true) { 
        regID = GCMRegistrar.getRegistrationId(this); 
        counter++; 
        if (counter == 100 || regID != null || !regID.equals("")) { 
         break; 
        } 
       } 
0

我已经解决了更改用于注册GCMRegistrar的项目。注册从项目名称到谷歌项目编号