GCM注册:移动网络上无法提供服务

时间:2014-04-05 07:19:19

标签: android google-cloud-messaging

我在GCM注册期间无法获得服务。

奇怪的是,当设备在Wifi上时,代码运行正常。在移动网络上几乎总是失败(90%)。

互联网上的移动数据运行良好,其他服务器运行正常,但与Wifi相比,它们有点慢。

我尝试添加一个简单的循环(不是指数等待)以在睡眠间隔后继续重试,但没有运气。

时刻我在操作(GCM注册)上切换Wifi成功。

请帮忙。

****更新更多信息** **:我在我的设备上测试了GCM Demo,它也有同样的问题。移动数据失败,在Wifi上运行良好。 失败的设备是注3,目标版本是4.4.2。 奇怪的是,相同的代码在其他设备上工作正常(版本2.3.6)

GCM注册

try {
                String sDeviceID=null;
                if (checkPlayServices()) {
                    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
                    sDeviceID = getRegistrationId(context);
                    if (sDeviceID==null) {
                        for(int i=0;i<60;i++){
                            try{
                                sDeviceID= gcm.register(SENDER_ID);
                                break;
                            }catch(IOException e){
                                Thread.sleep(2000);
                                continue;
                            }
                        }
                        // Persist the regID - no need to register again.
                        storeRegistrationId(context, sDeviceID);
                    }
                    sDeviceID = getRegistrationId(context);
                    if (sDeviceID==null) {
                        error=true;
                        throw new Exception("GCM Error");
                    }

显示XML                   

            <!-- 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.planetapp.schoolink" />
        </intent-filter>
    </receiver>

    <service android:name="com.planetapp.schoolink.GCMIntentService" />

广播接收器

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver  {
    @Override
    public void onReceive(Context context, Intent intent) {
    System.out.println("++++++++++++ON RECEIVE");
       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 */
           System.out.println("++++++++++++ID RECEIVED++++++++++++"+regId);

       }
    }

}

2 个答案:

答案 0 :(得分:4)

OP解决了他的问题并在评论中写了解决方案。解决方案:

Issue resolved. On my mobile, "Google Services" had setting "Restrict background data" checked. This was the reason why "Google Services Framework" or "Google Play Services" was not available when Wifi was turned off. Once I removed the restriction, I started working fine !

转到设置

寻找数据使用

enter image description here

寻找“Google Play服务”

enter image description here

当您进入另一个屏幕时:

  1. 菜单
  2. 限制BackgroundData(检查是否已选中并取消选中)。
  3. enter image description here

答案 1 :(得分:-1)

如果你使用Android模拟器,服务将无法使用

相关问题