Google云消息传递 - GCM - SERVICE_NOT_AVAILABLE

时间:2012-07-09 15:40:36

标签: android google-cloud-messaging service-not-available

我正在努力实施新的GCM,我正在关注Google 获得声明http://developer.android.com/guide/google/gcm/gs.html

我无法获取设备注册ID!

我的应用不断尝试与Google服务器连接,我的错误记录在这里:

onReceive: com.google.android.gcm.intent.RETRY
GCM IntentService class: com.dombox.app.GCMIntentService
Acquiring wakelock
[GCMIntentService] start
Registering app com.dombox.app of senders _my_sender_id_
Releasing wakelock
onReceive: com.google.android.c2dm.intent.REGISTRATION
GCM IntentService class: com.dombox.app.GCMIntentService
Acquiring wakelock
[GCMIntentService] start
handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
Registration error: SERVICE_NOT_AVAILABLE
Scheduling registration retry, backoff = 912617 (768000)
Releasing wakelock

这是我的活动代码,要求提供ID:

    try{
        Log.i(TAG, "[checkNotifRegistration] checkDevice");
        GCMRegistrar.checkDevice(this);
        Log.i(TAG, "[checkNotifRegistration] checkManifest");
        GCMRegistrar.checkManifest(this);
        if (GCMRegistrar.isRegistered(this)) {
            Log.i(TAG, "[checkNotifRegistration] reg id : "+GCMRegistrar.getRegistrationId(this));
        }
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
            // SENDER_ID is my project id into google account url
            GCMRegistrar.register(this, SENDER_ID);
            Log.i(TAG, "[checkNotifRegistration] reg id : "+GCMRegistrar.getRegistrationId(this));
        } else {
            Log.i(TAG, "[checkNotifRegistration] already registered as : " + regId);
        }
    } catch(Exception e){
        Log.e(TAG, "[checkNotifRegistration] Exception : "+e.getMessage());
        e.printStackTrace();
    }

这是我的服务代码

public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super(SENDER_ID);
           // SENDER_ID is my project id into google account url
    // TODO Auto-generated constructor stub
    Log.d(TAG, "[GCMIntentService] start");
}


public GCMIntentService(String senderId) {
    super(senderId);
    // TODO Auto-generated constructor stub
    Log.d(TAG, "[GCMIntentService] start - sender Id : "+senderId);
}
}

这是我的Android Manifest:

<permission android:name="com.dombox.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.dombox.app.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >

    <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="com.myapp.app" />
        </intent-filter>
    </receiver>

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

    <activity
        android:name=".activity.Myapp"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

我正在按照Google的说明操作,但我遇到了这个SERVICE_NOT_AVAILABLE错误,

我做错了什么?

6 个答案:

答案 0 :(得分:39)

问题与代码无关,但链接到测试手机。 测试手机没有SIM卡,时钟未设置。因此谷歌不能在错误的时间重新评估它。

我设法使用Android虚拟设备注册ID,使用google api,设置测试手机时钟

答案 1 :(得分:7)

android:enabled="true"  

删除此^

如果您尚未将其添加到清单

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8"

您不需要GCMIntentService中的第二个构造函数。只有这一个

public GCMIntentService() {         
 super(SENDER_ID);  }

在浏览Google代码网站并浏览GCM服务时,请确保您的SENDER_ID是从浏览器中复制的网址数。

答案 2 :(得分:3)

请记住,如果您在公司防火墙后面,可能不允许GCM使用的某些端口通过本地无线进行通信。我试图让我的模拟器最初工作时遇到了这个问题。我必须得到一个特殊的IP,允许通过下面列出的端口。


  

注意:如果您的组织有防火墙限制流量   进出Internet,您需要将其配置为允许   与GCM连接,以便您的Android设备接收   消息。要打开的端口是:5228,5229和5230.通常是GCM   仅使用5228,但有时使用5229和5230. GCM没有   提供特定的IP,因此您应该允许防火墙接受   到IP块中包含的所有IP地址的传出连接   在Google的ASN 15169中列出。

http://developer.android.com/google/gcm/gcm.html

答案 3 :(得分:2)

我遇到了这个问题...我的解决方案是禁用互联网连接并使用电话服务。

答案 4 :(得分:2)

我遇到了这个问题,这是由WIFI连接造成的。我关闭了WIFI并再次进行了测试(在3g上)并且工作正常。然后我重新打开WIFI并再次出错。回到3g,确保它再次起作用。

因此,最后,问题是由连接引起的。

答案 5 :(得分:1)

我在genymotion上运行我的应用程序时遇到此问题。像here一样安装google play服务,一切正常。