I am getting the error \"SERVICE_NOT_AVAILABLE\" on my GoogleCloudMessaging.register() call on a Android 2.2 device.
I am writing an app that uses GoogleCloudMessagi
So, there is another thing to watch out for that tripped me up.
I had it working fine in emulator + with test application, but then I had issues deploying it on actual devices.
Was consistently getting the SERVICE_NOT_AVAILABLE error, and resorted to an approach similar to the answer above:
IntentFilter filter = new IntentFilter("com.google.android.c2dm.intent.REGISTRATION");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Got into onReceive for manual GCM retrieval. "
+ intent.getExtras());
String regId = intent.getStringExtra("registration_id");
if (regId != null && !regId.isEmpty()) {
theResult.add(regId);
} else {
theResult.add("");
}
lastDitchEffortComplete = true;
}
};
cont.registerReceiver(receiver, filter);
This sort-of worked. But it was taking an oddly long amount of time to receive this information, and in a wait-loop I had, it would ONLY ever retrieve it AFTER I'd given up and let things proceed.
This got me thinking that there might be another cause to the SERVICE_NOT_AVAILABLE msg.
This is the code I had to do the fetching/registration with GCM:
new AsyncTask() {
final long timeSleep = 1000;
@Override
protected Void doInBackground(Void... params) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(cont);
String registrationId = "";
int count = 0;
while (count < 1) {
try {
count++;
Log.i(TAG, "Starting GCM Registration Attempt #" + count);
registrationId = gcm.register(CLOUDAPPID);
rph.storeRegistrationId(registrationId);
return null;
} catch (IOException e) {
e.printStackTrace();
}
Log.w(TAG, "Registration failed. Retrying in " + timeSleep + " ms.");
try {
Thread.sleep(timeSleep);
} catch (InterruptedException e) {
}
}
return null;
}
}.execute().get();
The main thing to look for here is the last line.
.execute.get();
A "clever/cheating" way to block on asynchronous off-thread results.
It turns out, that IS what is causing the problem. As soon as I just had:
.execute();
Let it pass through, and made the algorithm/code work with reporting back and not needing the registration id right away, then it was fine. I didn't even need the manual broadcast receiver, it just worked like it should.