我无法从Android GCM获取注册ID
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我无法从Android GCM获取注册ID相关的知识,希望对你有一定的参考价值。
虽然我尝试了其他关于这个问题的答案,但是无法解决。发件人ID正确,添加权限,API密钥为true。
我用这篇文章来创建项目: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();
它返回空字符串。
你有GCMIntentService
在你的应用程序的根包中定义了correctly吗?
<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)
是不可靠的,所以你应该避免它。
作为注册程序的一部分,id将通过广播回调到达您的GCMIntentService
。然后,您可以在任何地方存储gcm / fcm密钥,并在应用程序的其他部分(通常在服务器上)使用它。
如果您的应用程序第一次启动,则必须等待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);
G cm intent service.Java:
@Override
protected void onRegistered(Context c, String regId) {
// You can get it here!
}
编辑:较新版本的GCM库(与Google Play服务库捆绑在一起)等待响应并返回注册ID。所以这个答案适用于较旧的GCM库。
经过一天的努力,我遇到了同样的问题,我找到了解首先,我将我的GCM相关代码放入我的主程序包,并根据androidManifest.xml中的My Package进行更改
我举一个简单的例子。我的项目名称是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" />
注册发件人com.example.ilkgcm发件人“my_sender_id” - 此错误表明您没有将“my_sender_id”更改为有效的发件人ID,这将是一个12个字母的代码。
如果你没有得到注册的响应。主要原因之一是你的清单文件没有正确配置...特别是在正确的情况下给出“package_name”(你的应用程序包名称如com.xxx.yyy)。
例如,您的接收器类名称是:“MyCustomIntentService.java”,只需将此名称更改为“GCMIntentService.java”,然后再次尝试。这是简单的解决方案。只需更改SRC区域的文件名即可。
以上是关于我无法从Android GCM获取注册ID的主要内容,如果未能解决你的问题,请参考以下文章