5

I am getting SERVICE NOT AVAILABLE during GCM registration.

Strange behavior is that the code runs fine when device is on Wifi. Fails almost always (90 % times) when it is on mobile network.

Internet on mobile data is working fine , and other server operations are working, though they are bit slower compared to Wifi.

I tried to add a simple loop (not exponential wait) to keep retrying after a sleep interval, but no luck.

Moment I switch Wifi on the operation (GCM registration) succeeds.

Please help.

****Update More information** ** : I tested the GCM Demo on my device and it also has the same issue. Fails on mobile data, works fine on Wifi. Device where it is failing is Note 3, target version is 4.4.2. Strangely same code works fine on other device (version 2.3.6)

GCM Registration

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");
                    }

Manifest 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" />

Broadcast Receiver

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);

       }
    }

}

Mahesh
  • 81
  • 1
  • 1
  • 5
  • use this link http://stackoverflow.com/questions/17188982/how-to-fix-google-cloud-messaging-registration-error-service-not-available – Yogendra Apr 05 '14 at 09:09
  • 1
    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 ! – Mahesh Apr 05 '14 at 11:00
  • 1
    @Mahesh I have Restrict background data unchecked, but still facing the same issue as you reported. If i am correct i am referring to settings > Data Usage > option menu > Restrict background data. – Gem Oct 27 '14 at 12:26
  • @Mahesh, pls take care of answer [comments](http://stackoverflow.com/questions/22877855/gcm-registration-service-not-available-on-mobile-network/25945675?noredirect=1#comment43077970_25945675) – gangadhars Dec 05 '14 at 05:19

2 Answers2

4

OP resolved his issue and wrote solution in comment. Solution:

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 !

Go to Settings

Look for Data Usage

enter image description here

Look for "Google Play Services"

enter image description here

When you enter to another screen:

  1. Menu
  2. Restrict BackgroundData (Check if it is checked and uncheck it.)

enter image description here

surhidamatya
  • 2,419
  • 32
  • 56
gangadhars
  • 2,584
  • 7
  • 41
  • 68
-1

If you use android amulator, service wont be available

John Error
  • 2,076
  • 5
  • 18
  • 23