Android GCM error. Server returns Not Registered when client IS registered

user1657709 picture user1657709 · Dec 19, 2012 · Viewed 12.7k times · Source

I followed the demo given at http://developer.android.com/google/gcm/gs.html and got a trial application, GCMTrial to work correctly.

But, i tried to follow the same steps on an existing application but it didnot work. So I made an entirely new project. But even then , following the same exact steps, I couldnt get GCM to send a message successfully. So I tried renaming GCMTrial to the required name, and not that too does not work.

I register for GCM via the main activity and get the following log :

12-19 21:30:13.102: V/GCMRegistrar(15889): Registering receiver
12-19 21:30:13.112: D/GCMBaseIntentService(15889): handleRegistration: registrationId =         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX, error = null, unregistered = null
12-19 21:30:13.112: D/GCMRegistrar(15889): resetting backoff for com.XXX.XXX
12-19 21:30:13.117: V/GCMRegistrar(15889): Saving regId on app version 1

But when i try to send a GCM message, it returns the following error

[ errorCode=NotRegistered ]

Client Code :

public class MainActivity extends Activity {
TextView tve;
String TAG = "GCMTrial";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    final String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
        GCMRegistrar.register(this, "XXXXXX");
        Log.v(TAG, "Reg");
    } else {
        Log.v(TAG, "Already registered");
    }
}

GCMIntentService : 
public class GCMIntentService extends GCMBaseIntentService {
@Override
protected void onError(Context arg0, String arg1) {
    // TODO Auto-generated method stub
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub
    Log.d("GCM", "RECIEVED A MESSAGE");
    // Get the data from intent and send to notificaion bar
}
@Override
protected void onRegistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub
}

}

Manifest :

Server Code :

System.out.println("Sending GCM");
    String key = "XXX"; //Server API key taken from the site
    clientID = "XXXXXXXXX"; // copied from the logs
    Sender sender = new Sender(key);
    Message message = new Message.Builder().build();
    Result result = sender.send(message, clientID, 1);
    System.out.println(result.toString());

It is driving me crazy.. It shows as registered in the android phone, even calls the onRegistered method of the GCMIntentService class.. but when i try to send a message, it flags a "NotRegistered Error" ... I dont know what im doing wrong.. please help me out guys....

Answer

Dam picture Dam · Mar 14, 2013

Well, know it's a current bug when installing with adb.

GCMRegistrar does not know that the device is unregistered on GCM server and don't register.

What you can do is to register anyway :

if (regId.equals("")) {
        GCMRegistrar.register(this, "XXXXXX");
        Log.v(TAG, "Reg");
    } else {
        //I SAY BULLSHIT !
        GCMRegistrar.register(this, "XXXXXX");
        Log.v(TAG, "Reg");
    }

Or uninstal the app on client then run a new install from Adb.

Keep in mind there is few chances it happens in production.