查看 GCM 包附带的 gcm-demo-client 示例应用程序,我注意到使用了以下模式:
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
}
}
但是,如果由于某种原因,应用程序先前已成功注册到 GCM 和应用程序服务器,然后成功地从 GCM 注销但没有从应用程序服务器注销,那么应用程序将尝试在应用程序服务器上重新注册而不先注销,正确的?所以模式不应该更像:
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
}
}