Ani*_*yik 18 android google-cloud-messaging
虽然我尝试了其他关于这个问题的答案,但是无法解决.发件人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();
Run Code Online (Sandbox Code Playgroud)
它返回空字符串.
Ami*_*val 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>
Run Code Online (Sandbox Code Playgroud)
确保它"my_app_package"与您的应用程序的主程序包相同,否则您的id将返回一个空字符串.
还要注意
GCMRegistrar.register(this, "...");
Run Code Online (Sandbox Code Playgroud)
是异步的.因此,GCMRegistrar.getRegistrationId(this)在此之后立即呼叫是不可靠的,所以你应该避免它.
GCMIntentService作为注册程序的一部分,id将通过广播回调到达您的身份.从那里你可以将gcm密钥存储在你喜欢的任何地方,并在应用程序的其他部分使用它(通常在服务器上).
Eva*_*ack 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);
Run Code Online (Sandbox Code Playgroud)
GCMIntentService.java:
@Override
protected void onRegistered(Context c, String regId) {
// You can get it here!
}
Run Code Online (Sandbox Code Playgroud)
编辑:较新版本的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" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
44484 次 |
| 最近记录: |