I want to try Google Cloud Messaging (GCM) service, and I am faced with a problem at the beginning.
I get an error AUTHENTICATION_FAILED
while trying to register a device to GCM.
I searched and all I found were variations of the incorrect password. My password is correct and I am using just one account.
There are two ways to implement GCM client on Android:
- GCM library with additional jar, now deprecated.
- Google Play Services API
I started with the second of course and got this issue.
I thought the problem is in my phone, but then decided to try the first way, which worked!
However, it is deprecated and requires an additional jar, which doesn't seem like the right way.
In an attempt to understand the reasons for the error, I decompiled Google Play Services jar and compared it with GCM library.
It turns out they both have a similar method, something like:
void register(Context context, String senderIds) {
Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
intent.setPackage("com.google.android.gms"); // this one row are different
setPackageNameExtra(context, intent);
intent.putExtra("sender", senderIds);
context.startService(intent);
}
The difference in one row:
In GCM library it is com.google.android.gsf
, where gsf
is Google Services Framework (I guess), and it works!
In Google Play Services API jar it is com.google.android.gms
, And it does not work (AUTHENTICATION_FAILED error).
Then in GCM library I replaced "gsf" to "gms" and run. And I got the same AUTHENTICATION_FAILED error! If I enter another package, then it is not working.
What do I need to do to make it work? Should I set up something in the phone? Or is it a bug in Google Play Services? Have someone encountered such a problem?
Thanks in advance!
I ran into the same problem, and it doesn't seem like google is in any hurry to fix it.
I didn't want to add the deprecated client helper gcm.jar to my app, so I coded a minimal solution that works on my Android 2.3.6 Nexus One phone that fails registration as in the question above
try {
gcm = GoogleCloudMessaging.getInstance(context);
regID = gcm.register(SENDER_ID);
storeRegistrationId(regID);
msg = "Device registered, registration ID=" + regID;
sendRegistrationIdToBackend();
} catch (IOException ex) {
msg = "Exception registering for GCM :" + ex.getMessage();
// If there is an error, don't just keep trying to register.
oldSchoolRegister();
}
The AUTHENTICATION_FAILED triggers the IOException in the code above
private void oldSchoolRegister() {
Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
intent.setPackage("com.google.android.gsf");
setRegCallbackIntent(context, intent);
intent.putExtra("sender", SENDER_ID);
context.startService(intent);
}
private static synchronized void setRegCallbackIntent(Context context, Intent intent) {
regCallback = PendingIntent.getBroadcast(context, 0, new Intent(), 0);
intent.putExtra("app", regCallback);
}
public static synchronized void cancelRegCallbackIntent() {
if (regCallback != null) {
regCallback.cancel();
regCallback = null;
}
}
I added the above code to my app. They are simplified methods from the Client Helper gcm.jar (so you don't need to add the jar to your app)
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null && !extras.isEmpty()) { // has effect of unparcelling Bundle
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (messageType != null) {
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
showMessage(extras.getString("message")); // call your code
Logger.d(TAG, "Received message: " + message.alert + ": " + message.url);
} else if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
Logger.e(TAG, "Send error: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
Logger.e(TAG, "Deleted messages on server: " + extras.toString());
}
} else {
String regID = extras.getString("registration_id");
if (regID != null && !regID.isEmpty()) {
doRegistration(regID); // send to your server etc.
GCMSetup.storeRegistrationId(regID);
GCMSetup.cancelRegCallbackIntent();
}
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GCMBroadcastReceiver.completeWakefulIntent(intent);
}
This code is in the intent service, and has a few lines to store the ID received from GCM. As you can see only about 20 extra lines of code compared to a basic implementation, and no additional dependencies! You only need to update your AndroidManifest.xml to make sure you can receive the REGISTRATION intent.
<receiver android:name="com.camiolog.android.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="com.camiolog.android"/>
</intent-filter>
</receiver>
I hope this helps until google gets their act together!
This seems like a bug. Here's what an Android developer says about it in the android-gcm Google Group:
Some background: Froyo and Gingerbread registration is implemented in
GoogleServicesFramework, using the Google account for registration.
This has resulted in a lot of auth errors for people where the account
was not in a good state.
Starting with ICS, GCM doesn't depend or uses the Google account - you
can use it before you add an account or without any accounts.
The "Play Services" update is implementing the new scheme on all
devices - but it seems a small number of devices have problems with
this, we're investigating - but the numbers are far lower than those
with the old scheme.
If you want to use the code in GSF, for Froyo and Gingerbread - you
need to use the previous library, which sets package name explicitly.
The new library in GCM is using the new registration code.
The actual connection to google is following the same path - we're
gradually (and slowly) moving devices to the new code in play
services.
So far I have 2 bugreports, and we have a few suspects. We know that
if a device is not connected for >9 months it's going to be in this
state, and a factory reset will be needed.
We had some reports where a factory reset didn't solve the problem -
but I have no bugreport or information to confirm or trace this. The
only case I identified where a factory reset wouldn't help is if the
phone is sending bad information to the server in the initial checkin
- we're adding extra checks for this.
Apparently, a factory reset may solve the problem, but they're still investigating.
So it looks like the solution to avoid this problem is to fall back to the old deprecated GCM client library in case the AUTHENTICATION_FAILED error happens on FROYO and GINGERBREAD.
Here is a simple code snippet of how I upgraded the new Gcm client to fallback to use the old client:
@Override
protected void onPostExecute(Integer resultCode) {
if(resultCode == EXCEPTION_THROWED) {
//Android 2.2 gmc bug http://stackoverflow.com/questions/19269607/google-cloud-messaging-register-authentication-failed
//fall back to old deprecated GCM client library
GCMRegistrar.checkDevice(StartActivity.this);
GCMRegistrar.checkManifest(StartActivity.this);
final String registrationId = GCMRegistrar.getRegistrationId(StartActivity.this);
if (registrationId.equals("")) {
GCMRegistrar.register(StartActivity.this, SENDER_ID);
}
//Toast.makeText(context, "Orders and menus won't be sync with other devices since GoogleCloudMessaging is not working correctly on this device. Please notify the developer.", Toast.LENGTH_LONG).show();
}
}
You can find the old deprecated GCM Client helpher here: http://developer.android.com/google/gcm/helper.html
You can find the code of the GCM client on your computer on the path:
ANDROID_SDK_ROOT/extras/google/gcm-client (given you've downloaded this extra using the Android SDK Manager).
I put the old gcm client in a new package named com.google.android.gcm.deprecated to try to remember myself to not use this for other stuff.