我在验证GCM令牌方面有些困惑。我正在使用Sencha框架开发跨平台应用程序,我的服务器端是Java。我有一个关于如何验证注册ID(GCM令牌)的问题?是否有特定的API来验证GCM令牌?您能指导我如何处理这个问题,无论是在客户端还是服务器端?我已经在服务器端完成了注册部分,用户可以在数据库中注册他们的GCM令牌。现在我需要验证这个注册令牌。
每两周注销应用程序是一个好的方法吗?
每两周注销应用程序是一个好的方法吗?
您可以在客户端注册和注销GCM。这是从Google获取注册ID的地方。
一旦您获得了注册ID,应该将其视为有效,直到:
您使用注册ID向Google的GCM服务器发送消息并收到NotRegistered或InvalidRegistration错误。在这些情况下,您应该从数据库中删除注册ID。
您使用注册ID向Google的GCM服务器发送消息并收到成功响应,但响应包含规范注册ID。在这种情况下,您应该用规范注册ID替换注册ID。
应用程序明确从GCM注销,并通知服务器进行注销,在这种情况下,您应该从数据库中删除注册ID。
我不认为每两周注销应用程序有任何意义。 Google的代码示例只在安装新版本后重新注册应用程序一次,即使在重新注册之前也不会注销。
private void asyncSend(List<String> partialDevices) {
// make a copy
final List<String> devices = new ArrayList<String>(partialDevices);
threadPool.execute(new Runnable() {
public void run() {
Message message = new Message.Builder().build();
MulticastResult multicastResult;
try {
multicastResult = sender.send(message, devices, 5);
} catch (IOException e) {
logger.log(Level.SEVERE, "Error posting messages", e);
return;
}
List<Result> results = multicastResult.getResults();
// analyze the results
for (int i = 0; i < devices.size(); i++) {
String regId = devices.get(i);
Result result = results.get(i);
String messageId = result.getMessageId();
if (messageId != null) {
logger.fine("Succesfully sent message to device: " + regId +
"; messageId = " + messageId);
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// same device has more than on registration id: update it
logger.info("canonicalRegId " + canonicalRegId);
Datastore.updateRegistration(regId, canonicalRegId);
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister it
logger.info("Unregistered device: " + regId);
Datastore.unregister(regId);
} else {
logger.severe("Error sending message to " + regId + ": " + error);
}
}
}
}});
}