Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCM registration : SERVICE NOT AVAILABLE on mobile network

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

       }
    }

}

like image 833
Mahesh Avatar asked Oct 21 '22 10:10

Mahesh


1 Answers

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

like image 68
2 revs, 2 users 79% Avatar answered Oct 23 '22 02:10

2 revs, 2 users 79%