【发布时间】:2012-07-21 16:10:16
【问题描述】:
按照GCM: Getting Started 指南的最后一部分,在收到结果后需要做一些记账。
引自指南:
现在需要解析结果并在以下情况下采取适当的措施:
- 如果消息已创建,但结果返回了规范的注册 ID,则需要替换当前注册
带有规范的 ID。- 如果返回的错误是 NotRegistered,则需要删除该注册 ID,因为应用程序已从 设备。
这是处理这两个条件的代码 sn-p:
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。 如果这两个列表相互关联(即保留顺序和大小),则没有记录。
我如何确定哪个结果是指哪个设备?
-- 更新
我已将解决方案的 sn-p 粘贴到下面的单独答案中
【问题讨论】:
标签: android google-cloud-messaging