在GCM 的最后一部分:入门指南之后,在收到结果后需要做一些簿记。
引用指南:
现在有必要在以下情况下解析结果并采取适当的措施:
- 如果消息已创建,但结果返回了规范注册 ID,则需要将当前注册
ID 替换为规范注册 ID。- 如果返回的错误是 NotRegistered,则需要删除该注册 ID,因为该应用程序已从设备上卸载。
这是处理这两个条件的代码片段:
if (result.getMessageId() != null) { String canonicalRegId = result.getCanonicalRegistrationId(); if (canonicalRegId != null) { // same device has more than on registration ID: update database } } else { String error = result.getErrorCodeName(); if (error.equals(Constants.ERROR_NOT_REGISTERED)) { // application has been removed from device - unregister database } }
上面的指南指的是单个结果,而不是多播案例。我不确定如何处理多播情况:
ArrayList<String> devices = new ArrayList<String>();
for (String d : relevantDevices) {
devices.add(d);
}
Sender sender = new Sender(myApiKey);
Message message = new Message.Builder().addData("hello", "world").build();
try {
MulticastResult result = sender.send(message, devices, 5);
for (Result r : result.getResults()) {
if (r.getMessageId() != null) {
String canonicalRegId = r.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// same device has more than on registration ID: update database
// BUT WHICH DEVICE IS IT?
}
} else {
String error = r.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister database
// BUT WHICH DEVICE IS IT?
}
}
}
} catch (IOException ex) {
Log.err(TAG, "sending message failed", ex);
}
我提交了一份设备列表,并收到了一份结果列表。Result 对象不包含注册 id,但如果第一个已过时,则仅包含规范 id。如果这两个列表是相互关联的(即保留顺序和大小),则没有记录。
我如何确定哪个结果是指哪个设备?
- 更新
我在下面的单独答案中粘贴了解决方案的片段