在Android 2.2 GCM SERVICE_NOT_AVAILABLEAndroid、SERVICE_NOT_AVAILABLE、GCM

2023-09-12 00:34:57 作者:猪是看来过倒

我让我的GoogleCloudMessaging.register错误SERVICE_NOT_AVAILABLE()一个Android 2.2的设备上调用。

I am getting the error "SERVICE_NOT_AVAILABLE" on my GoogleCloudMessaging.register() call on a Android 2.2 device.

我写一个应用程序,采用采用全新的谷歌游戏服务GoogleCloudMessaging。我实现了它使用提供了对Android网站上的指导和我的源代码包含了很多错误检查和处理code如确保了谷歌播放服务的安装或更新。该GCM注册code也可以实现的建议之一,落实处理SERVICE_NOT_AVAILABLE误差的指数退避如谷歌。

I am writing an app that uses GoogleCloudMessaging using the new Google Play Services. I implemented it using the guidelines provided on the Android website and my source contains a lot of error checking and handling code such as making sure the Google Play Services is installed, or updated. The GCM registration code also implements a exponential backoff as google as suggested one to implement to handle the SERVICE_NOT_AVAILABLE error.

我已经在设备上,包括ICS,JB,蜂巢,甚至2.3.x设备的广泛测试了我的申请。该GCM登记工作,我能够通过GCM发送消息给它。然而,一个2.2的设备上,我不断地收到对GoogleCloudMessaging.register一个SERVICE_NOT_AVAILABLE错误()与替代指数退避甚至打电话

I have tested my application on a wide array of devices including ICS, JB, Honey Comb and even 2.3.x devices. The GCM registration works and I am able to send messages to it via GCM. However on a 2.2 device I am continuously getting a SERVICE_NOT_AVAILABLE error on the GoogleCloudMessaging.register() call even with the exponential backoff in place.

这GCM失败上的设备是三星SGH-I896是准确的,并在手机上有2谷歌帐户。我读过这个错误可能是由于配置错误的时间造成的,但时间被设置为自动。这款手机是不是moded和运行三星股票ROM。

The device that GCM is failing on is a Samsung SGH-I896 to be exact and the phone has 2 google accounts on it. I've read that this error might be caused by a misconfigured time, but the time is set to be automatic. The phone is not moded and is running samsung stock ROM.

我也曾尝试重新启动设备,以及重新安装谷歌播放服务没有运气。在这个问题上的任何帮助,将大大AP preciated。

I have also tried rebooting the device as well as reinstalling Google Play Services without luck. Any help on this issue will be greatly appreciated.

编辑:我试图执行GCM使用旧gcm.jar和GCMRegistrar,它结束了工作的设备上。不过我几乎认为这是一个很好的解决问题,因为谷歌已经停止支持该方法AFAIK。

I tried to implement GCM using the old gcm.jar and GCMRegistrar and it ended up working on the device. However I hardly consider this a good solution to the problem as Google has stopped supporting this method afaik.

EDIT2:请参阅接受的答案

See the accepted answer.

推荐答案

我经历了同样的问题。 GCM工作正常,在我的平板电脑运行Android 4.04,但总是收到运行在我的智能手机一个SERVICE_NOT_AVAILABLE的Andr​​oid 2.3。

I experienced the same problem. GCM works fine on my Tablet running Android 4.04, but always received a SERVICE_NOT_AVAILABLE on my smartphone running Android 2.3.

我发现以下解决方法不使用(据我所知)任何德precated类。添加动作com.google.android.c2dm.intent.REGISTRATION的GCMBroadcastReceiver在你的清单。这将使接收registration_id你GCMBroadcastReceiver。

I found following workaround not using (so far as I know) any deprecated classes. Add the action "com.google.android.c2dm.intent.REGISTRATION" to the GCMBroadcastReceiver in your manifest. This will enable to receive the registration_id at your GCMBroadcastReceiver.

<receiver
   android:name="YOUR_PACKAGE_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="YOUR_PACKAGE_NAME" />
      </intent-filter>
</receiver>

在,你的GCMBroadcastReceiver是能够接收registration_id:

After that your GCMBroadcastReceiver is able to receive the registration_id:

public void onReceive(Context context, Intent intent) {
   String regId = intent.getExtras().getString("registration_id");
   if(regId != null && !regId.equals("")) {
      /* Do what ever you want with the regId eg. send it to your server */
   }
}

虽然我仍然获得了SERVICE_NOT_AVAILABLE错误,我可以处理我GCMBroadcastReceiver的registration_id,我能够将消息发送到我的智能手机。很奇怪,但它为我工作。

Although I still get a SERVICE_NOT_AVAILABLE error, I can handle the registration_id in my GCMBroadcastReceiver and I am able to send messages to my smartphone. Quite weird, but it works for me.