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