Java GCM服务不可用错误
我在android应用程序中使用GCM,当我尝试用GCM注册设备时(使用命令Java GCM服务不可用错误,java,android,google-cloud-messaging,Java,Android,Google Cloud Messaging,我在android应用程序中使用GCM,当我尝试用GCM注册设备时(使用命令GCM.register(SENDER\u ID)),我得到了服务不可用的错误,我尝试了别人建议的方法(接受的解决方案),我确实得到了注册ID 为什么会这样 Android清单: <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.appspot.smartgan" android
GCM.register(SENDER\u ID)
),我得到了服务不可用的错误,我尝试了别人建议的方法(接受的解决方案),我确实得到了注册ID
为什么会这样
Android清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appspot.smartgan"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="com.appspot.smartgan.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.appspot.smartgan.permission.C2D_MESSAGE" />
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light" >
<activity
android:name="com.appspot.smartgan.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".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="com.appspot.smartgan" />
</intent-filter>
</receiver>
<service
android:name=".GcmIntentService"
android:enabled="true" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.appspot.smartgan.LoginActivity"
android:label="@string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateVisible" >
</activity>
<activity
android:name="com.appspot.smartgan.ChildActivity"
android:label="@string/title_activity_child" >
</activity>
</application>
</manifest>
注册方法:
private void registerInBackground() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String message = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
message = "Device registered, registration ID=" + regid;
Log.d("SMARTGAN_PLAY", "register completed");
// send registration id to the server
sendRegistrationIdToServer();
// Persist the regID - no need to register again.
storeRegistrationId(context, regid);
} catch (IOException e) {
message = "Error:" + e.getMessage();
}
return message;
}
}.execute(null, null, null);
}
private void registerInBackground(){
新建异步任务(){
@凌驾
受保护字符串doInBackground(无效…参数){
字符串消息=”;
试一试{
如果(gcm==null){
gcm=GoogleCloudMessaging.getInstance(上下文);
}
regid=gcm.寄存器(发送方ID);
message=“设备已注册,注册ID=“+regid;
Log.d(“智能游戏”,“注册完成”);
//将注册id发送到服务器
sendRegistrationIdToServer();
//坚持注册-无需再次注册。
storeRegistrationId(上下文,regid);
}捕获(IOE异常){
message=“Error:”+e.getMessage();
}
返回消息;
}
}.执行(空,空,空);
}
如果您收到注册ID,则表示您已将设备成功注册到GCM
编辑:
以下是一个很好的GCM教程:
我的GCM是建立在我上面链接的教程的基础上的,所以如果你通读了它,它应该对你有用,它看起来离你不远了。我会说,清单是非常挑剔的,你需要确保你的包链接充分
另外,您不需要注册意图我认为问题在于您的Google API项目中提到的允许IP地址。
请检查项目的详细信息,并删除
限制使用IP地址
下的IP地址(如果存在)。我也遇到了类似的问题。我知道,但是当gcm.register()
生成IOException时,如果没有看到代码的副本,我真的帮不了你很多忙,所以我确切地知道你把register命令放在哪里。我编辑了我的答案,并链接到一个帮助我工作的教程。如果你能用你的代码编辑你的问题,我将能提供更多帮助。调用register函数的activity类很长,因此我不能在这里添加所有内容,但是我遵循了谷歌发布的示例项目,所以你可以在那里看到我的代码,这是一样的。老实说,伙计,我们真的帮不了你,除非我们看到一些代码,我们只是在黑暗中刺伤。我觉得这可能是一个清单问题,我的所有问题都源于在清单中错误地设置接收者
<receiver
android:name="rockit.app.beardallstreetprimary.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="rockit.app.beardallstreetprimary" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gcm.GCMIntentService" android:enabled="true" />