Taking a look at the gcm-demo-client sample application that comes with the GCM package, I notice that the following pattern has been used:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
// Register application with GCM
} else {
// Device is already registered on GCM, check server.
if (GCMRegistrar.isRegisteredOnServer(this)) {
// Skip registration.
} else {
// Try to register again on server
}
}
But what if, for some reason, the application was previously succesfully registered to GCM and the application server, then unregistered from GCM succesfully but not from the application server, then the application would try to re-register on the application server without unregistering first, right? So shouldn’t the pattern be more like:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
// Check if device was previously registered with application server
if (GCMRegistrar.isRegisteredOnServer(this)) {
// Unregister on application server
}
// Register application with GCM
} else {
// Device is already registered on GCM, check server.
if (GCMRegistrar.isRegisteredOnServer(this)) {
// Skip registration.
} else {
// Try to register again on server
}
}
You could do it that way, but because the unregistration is (or should be) an asynchronous event, you would have to trigger a special unregistration that would then register with GCM after completing.
So something like:
I’m not sure that you should worry about it, because it shouldn’t be that big of a deal to your application server if the same device registers again since the registration ID provided by GCM will likely be the same. But if it’s not the next time you send a message GCM should give solve the problem for you by returning a canonical Id.
You could also only unregister from GCM if you successfully unregister from your application server first.